Notes on shell scripting.

Bash is a Unix shell and command language originally released in 1989. It can read and execute text files called shell script that are useful for automating tasks.

The terminal cheatsheet contains some of the commands that can be run as part of a bash script.

Running a script

Navigate to the directory containing the script and use ./scriptname.sh to execute it.

If the script fails with an permissions error, use chmod 755 scriptname.sh to make it executable.

#!
Shebang. Indicates which interpreter a script should be run with: #!/bin/bash

$1, $2, ...
The first, second, etc command line arguments to the script.

variable=value
To set a value for a variable. Remember, no spaces on either side of =

Quotes " '
Double will do variable substitution, single will not.

if [ ] then else fi
Perform basic conditional logic.

Evaluating the arguments passed to a script

#!/bin/bash
# case example
case $1 in
	start)
		echo starting
		;;
	stop)
		echo stoping
		;;
	restart)
		echo restarting
		;;
	*)
		echo don\'t know
		;;
esac

Links

incoming(4) | meta | tablet | terminal shortcuts | photography