Shell Scripts






Command Line Arguments
command
arg1
arg2
arg3
arg4
arg5
arg6
arg7
arg8
arg9
$0
$1
$2
$3
$4
$5
$6
$7
$8
$9

$#
the number of command line arguments
$*
all the command line arguments i.e. echo "$*" => echo "$1 $2..."
$@
all the command line arguments i.e. echo "$@" => echo "$1" "$2"...
shift
promotes each argument one position to the left

Debugging (+flag = on -flag = off)
-x
print ech line just before it executes
-v
prints the input lines as they are read
-n
reads commands but does not execute them
-u
teats unset variables as an error when substituting

Test Command
$ test expression or
$ [ expression ]
-r file
true if file exists and is readable
-w file
true if file exists and is writable
-x file
true if file exists and is executable
-f file
true if file exists and is a regular file
-d file
true if file exists and is a directory
-c file
true if file exists and is a character special file
-b file
true if file exists and is a block special file
-p file
true if file exists and is a named pipe special file
-u file
true if file exists and has set-user-id bit set
-g file
true if file exists and has set-group-id bit set
-k file
true if file exists and has sticky bit set
-s file
true if file exists and has a size greater than zero
-t [filedes]
true if the open file in slot fildes (file descriptor) is associated with a terminal
-z s1
true if the length of string s1 is zero
-n s1
true if the length of string s1 is non-zero
s1 = s2
true if strings s1 and s2 are identical
s1 != s2
true if strings s1 and s2 are not identical
s1
true if s1 is not the null string
n1 -eq n2
true if the integers n1 and n2 are algebraically equal
-lt less than
-le less than or equal
-gt greater than
-ge greater than or equal
-ne not equal
-eq equal
!
negation
-a
binary and
-o
binary or (-a has higher precedence)
(expression)
parentheses for grouping

Control Structure
if then else
if commands_to_test; then
commands
else
commands
fi
if then elif
if commands_to_test; then
commands
elif commands_to_test; then
commands
else
commands
fi
for in loop
for loop_index in arg_list; do
commands
done
while loop
while commands_to_test; do
commands
done
until loop
until commands_to_test; do
commands
done
break (for, while, until)
transfers control immediately to the expression after the nearest done
continue (for, while, until)
bring execution to top of loop
case statement
case test-string in
pattern-1) commands-1
;;
pattern-2) commands-2
;;
pattern-3) commands-3
;;
esac


Page Created May 31st, 2004