Shell Programming: Introduction
Shell Scripts
- A shell script (or shell program) is
a series of Unix commands placed in an ASCII text file.
- Each shell (
ksh, csh, sh,
...) provides mechanisms for control (e.g., statements like
if, while, for, ...).
- Unix commands + shell variables + control mechanisms =
powerful programming language.
- See Chapter 4 (Nutshell)
or Chapter 12 (Sobell) for
ksh information.
Executing Shell Scripts
A Simple Example
Remember Shell Variables?
- A shell variable can have any name of the form
[A-Za-z][A-Za-z0-9_]*.
- For
ksh:
-
variable=value
- Notice that there are no spaces on either side of the equals sign.
-
export variable=value
- Using
export makes the value visible to
child processes.
Using Variables
Some Basic Commands
- For output, use "
print" or "echo":
$ print "Hello World"
Hello World
- At the end of your script, use "
return N" to
give a specific return value, as in this script:
#!/usr/local/bin/ksh
print "Hello World"
return 0
- You can use the built-in variable
$? to access
the exit status of the previously run command. If the above
script were HelloScript:
$ HelloScript
$ print $?
0
If Statements
- The basic form for if statements in ksh is:
if condition
then
statements
[elif condition
then
statements]
[else
statements]
fi
- The
then/else/elif/fi keywords take the place
of braces ({})---braces mean something special to the shell!
- Examples of condition are (assuming
integer a
b):
(( a == 10 ))
(( b < 20 ))
[[ $person = steve ]]
[[ $person != todd ]]
(( (a < 10) || (a > 100) ))
[[ ($person != steve ) && ($person != todd ) ]]
- The
elif or else parts can be omitted.
Example If Statement
if [[ $person = steve ]]
then
print $person is on the sixth floor.
elif [[ $person = todd ]]
then
print $person is on the fifth floor.
elif [[ $person = markus ]]
then
print $person is on the fifth floor.
else
print "Who are you talking about?"
fi
While Statements
- Basic form:
while condition
do
statements
done
- condition has the same syntax as for if statements.
- You can use
break or continue
or return inside a loop with the same meaning as in C.
- Example:
#!/usr/local/bin/ksh
# Report type of executable file anywhere in search path.
#
path=$PATH
dir=${path%%:*}
while [[ -n $path ]]; do
if [[ -x $dir/$1 && ! -d $dir/$1 ]]; then
file $dir/$1
return
fi
path=${path#*:}
dir=${path%%:*}
done
print "File not found."
return 1
Case Selection
Sample Case
case $person in
steve)
print "He's on the sixth floor." ;;
todd | markus)
print "He's on the fifth floor." ;;
*)
print I do not know $person. ;;
esac
For Loops
- A
for loop can be used to iterate over all items in
an array or list.
- Basic form:
for var [in list]
do
statements
done
- If
in list is omitted in a script, the list is assumed
to be $* --- all the arguments to the script.
- Examples:
for name in Jack Jill John Jane Dick
do
print "Next person is $name."
done
integer count=0
for arg in $@
do
let count='count+1'
print "argument $count: $arg"
done
for i in *.for
do
mv $i ${i%.for}.f
print ${i%.for}.f
done