UNIX Scripting
A summary of useful tips and tricks as can be found on different (web)-pages such as:
- Russel Quong [1], A guide for writing scripts.
- Mendel Cooper [2], Advanced Bash Scripting Guide. An in-depth exploration of the art of shell scripting.
Debugging Scripts
- Use echo statements.
- Run bash -n script to check for syntax errors.
- Use the command set -v to get a verbose dump of each line the shell reads.
Use set +v to turn off verbose mode. - Use the command set -x to see what each command expands to.
Again, set +x turns this mode off.
Command line arguments
The command line parameters to a script are stored in the nearly identical variables $* and $.
The following table summarizes the variables you would use for command line processing.
For the example values, assume you wrote a bash script /usr/bin/args.sh and ran it as shown below.
| Variable | Meaning | Example: echoArgs -t two "let's go" |
|---|---|---|
| $* | Command line args | -t two let's go |
| $@ | Command line args | -t two "let's go" |
| $# | Number of args | 3 |
| $0 | Name of script | /usr/bin/args.sh |
| $1 | First arg in $* | -t |
| $2 | Second arg in $* | two |
| $3 | Third arg in $* | let's go |
| $4 | Fourth arg in $* | (empty) |
echoArgs () {
echo $#
for i in "$@"; do
echo "($i)";
done;
for i in $*; do
echo "(($i))";
done
}
$ echoArgs -t two "let's go"
3
(-t)
(two)
(let's go)
((-t))
((two))
((let's))
((go))
Command Line Options
I prefer the usage of case statements in stead of the getops.
| Flag | Description |
|---|---|
| -o OUT | Sends output to file OUT |
| -n | Shows what you would do but do not do it |
| -v | Gives more output, each -v increases verboseness |
| -l | Same as -verbose |
| -version | Shows the version and quit |
nflag=0
vlevel=0
OUT=
while [ $# -gt 0 ]
do
case "$1" in
-o ) OUT=$2 ; shift 2 ;;
-n ) nflag=1 ; shift ;;
-l | -v ) vlevel=$(( vlevel+1 )) ; shift ;;
-ver* ) echo "Version $version" ; exit 1 ;;
* ) echo "Saw non flag $arg" ; break ;;
esac
done
Tests
UNIX tests have the format:
if [ 0 ] # zero then echo "0 is true." else # Or else ... echo "0 is false." fi
Suppose you want to check if the first parameter (a filename) exists then:
if [ -e $1 ] then echo "File $1 exists" else echo "File $1 does not exist" fi
Tests overview
| Test | Description |
|---|---|
| -a | File exists. Deprecated, its use is discouraged. |
| -b | File is a block device [3]. |
| -c | File is a character device [4]. |
| -d | File is a directory. |
| -e | File exists. The file maybe a file or a directory. Use other operators to find out. |
| -f | File is a regular file (not a directory or device file). |
| -g | File exists and its set-group-id bit is set. The file maybe a file or a directory [5]. |
| -h | File is a symbolic link. |
| -i | Not used |
| -j | Not used |
| -k | File exists and its sticky bit is set [6]. |
See also
- Shell-Differences Short overview of different shells
- Overview, Short overview of different shells
- Csh Whynot, List of csh problems
Reference
- ↑ Russel Quong, A guide to writing shell scripts for C/C++/Java and unix programmers.
- ↑ Mendel Cooper Advanced Bash-Scripting Guide. Very extensive Guide with a lot of examples.
- ↑ Block special files or block devices provide buffered access to the hardware, such that the hardware characteristics of the device are not visible.
- ↑ Character special files or character devices provide unbuffered, direct access to the hardware device.
- ↑ ToDo Group Id.
- ↑ Wikipedia, The sticky bit is a user ownership access right flag that can be assigned to files and directories on Unix-like systems. The sticky bit can be set using the chmod command and can be set using its octal mode 1000 or by its symbol t (s is already used by the setuid bit).