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
There are several testing categories:
Test for Files
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 exists and is a symbolic link. |
-i | Not used |
-j | Not used |
-k | File exists and its sticky bit is set [6]. |
-L | File exists and is a symbolic link. |
-p | File exists and is a named pipe (FIFO). |
-r | File exists and is readable. |
-s | File exists and has a size larger than zero. |
-S | File exists and is a socket. |
-t[n] | The open file is associated with a terminal device. Default value for n is 1. |
-u | File exists and its set-user-bit is set. |
-x | File exists and is executable. |
Test for Strings
Test | Description |
---|---|
string | String is not null. |
-n s1 | String s1 has nonzero length. |
-z s1 | String s1 has zero length. |
s1 = s2 | String s1 and s2 are identical. String s2 can be a wildcard pattern. Quote s2 to treat it literally. Although correct do not use!. Better use ==, see below [7]. |
s1 == s2 | String s1 and s2 are identical. String s2 can be a wildcard pattern. Quote s2 to treat it literally. Preferred above =. |
s1 != s2 | String s1 and s2 are not identical. String s2 can be a wildcard pattern. Quote s2 to treat it literally. |
s1 =~ s2 | String s1 matches extended regular expression s2. Quote s2 to keep the shell from expanding embedded shell meta-characters. Strings matched by parentheszied sunexpressions are placed into elements of the BASH_REMATCH array [8]. |
s1 < s2 | ASCII value of s1 precedes that of s2. Only valid when used inside [[ ]] construct. |
s1 > s2 | ASCII value of s1 follows that of s2. Only valid when used inside [[ ]] construct. |
Internal Test
Test | Description |
---|---|
-o opt | Option opt' for set -o is on. [9]. |
Test for Integers
Test | Description |
---|---|
n1 -eq n2 | n1 equals n2. |
n1 -ge n2 | n1 greater than or equal to n2. |
n1 -gt n2 | n1 greater than n2. |
n1 -le n2 | n1 less than or equal to n2. |
n1 -lt n2 | n1 less than n2. |
n1 -ne n2 | n1 not equal n2. |
Combined Forms
Test | Description |
---|---|
(condition) | True if condition is true (used for grouping). For test and [], the ()s should be quoted by a \. The form [[ ]] does not require quoting the parentheses. |
! condition | True if the condition is false. |
c1 -a c2 | True if both conditions are true. |
c1 && c2 | True if both conditions are true. Only valid within a [[ ]] construct. |
c1 -o c2 | True if either condition is true. |
c1 || c2 | True if either condition is true. Only valid within a [[ ]] construct. |
Set options
Option | Name | Alternative | ksh88 | ksh93 | Bash | Description |
---|---|---|---|---|---|---|
-a | allexport | -o allexport | x | x | x | Exports all defined variables. |
+A name | x | x | Assigns remaining arguments as elements of array name. | |||
-A name | x | x | Same as +A name, but unset name before making assignments. | |||
-b | notify | -o notify | x | x | Prints job completion message as soon as the jobs terminate; don't wait until the next prompt. | |
-B | brace expansion | -o braceexpand | x | Enables brace expansion. On by default. | ||
-C | noclobber | -o noclobber | x | Prevents overwriting via > redirection; use >| to overwrite files. | ||
-e | errexit | -o errexit | x | x | x | Exits if a command yields a nonzero exit status. The ERR trap executes before the shell exits. |
-E | x | Causes shell functions, command substitutions and subshells to inherit the ERR trap. | ||||
-f | noglob | -o noglob | x | x | x | Ignores filename metacharacters (Globbing disabled). |
-G | globstar | -o globstar | x | Causes ** to also match subdirectories during filename expansion. | ||
-h | hashall | -o hashall | x | Locates commands as they are defined. Bash hashes command names. On by default. | ||
-h | trackall | -o trackhall | x | x | Locates commands as they are defined. Korn shell creates tracked aliases. On by default. | |
-k | keyword | -o keyword | x | x | x | Assignment of environment variables (var=value) takes effect regardless of where they appear on the command line. Normally assignments must precede the command line. |
-m | monitor | -o monitor | x | x | x | Enables job control. Background jobs executes in a separate process group. Usually -m is set automatically. |
-n | noexec | -o noexec | x | x | x | Reads commands in script, but do not execute them (syntax check). Both shells ignore this option if it interactive. |
-p | privileged | -o privileged | x | x | x | Start up as privileged user.
|
-s | x | x | Sorts the positional parameters. | |||
-t | onecmd | -o onecmd | x | x | x | Exits after the execution of one command. |
-T | functrace | -o functrace | x | Causes shell functions, command substitutions and subshells to inherit the DEBUG trap. | ||
-u | nounset | -o nounset | x | x | x | Attempt to use undefined variable outputs error message, and forces an exit. |
-v | verbose | -o verbose | x | x | x | Prints each command to stdout before executing it. |
-x | xtrace | -o xtrace | x | x | x | Similar to -v, but expands commands (preceded by the value of PS4. Provides step-by-step tracing of shell scripts. |
- | x | x | x | End of options flag. All other arguments are positional parameters. | ||
-- | x | x | x | Unset positional parameters. If arguments given (-- arg1 arg2), positional parameters set to arguments. |
The +o [mode] disables the given shell option, while the -o [option] enables the given shell option.
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.
- ↑ The setuid and the setguid can be set using the chmod command. The difference is the u resp. g in the chmod gu+s.
- ↑ 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). To set use chmod +t <filename>, to delete chmod -t <filename>
- ↑ A single equal sign is used for instantiating a variable and should therefore not also be used for comparison
- ↑ [ToDo BASH_REMATCH]
- ↑ See the BASH options for full description of the 'set -o.