CHAPTER 9 Shell Programming
fcn () { command; }
where the space after {, and the semicolon (;) are both required; the latter can be dispensed with if a <newline> precedes the }. Additional spaces and <newline>'s are allowed. We saw a few examples of this in the sample .profile in an earlier chapter, where we had functions for ls and ll:
ls() { /bin/ls -sbF "$@";}
ll() { ls -al "$@";}
The first one redefines ls so that the options -sbF are always supplied to the standard /bin/ls command, and acts on the supplied input, "$@". The second one takes the current value for ls (the previous function) and tacks on the -al options.
Functions are very useful in shell scripts. The following is a simplified version of one I use to automatically backup up system partitions to tape.
#!/bin/sh
# Cron script to do a complete backup of the system
HOST=`/bin/uname -n`
admin=frank
Mt=/bin/mt
Dump=/usr/sbin/ufsdump
Mail=/bin/mailx
device=/dev/rmt/0n
Rewind="$Mt -f $device rewind"
Offline="$Mt -f $device rewoffl"
# Failure - exit
failure () {
$Mail -s "Backup Failure - $HOST" $admin << EOF_failure
$HOST
Cron backup script failed. Apparently there was no tape in the device.
EOF_failure
exit 1
}
# Dump failure - exit
dumpfail () {
$Mail -s "Backup Failure - $HOST" $admin << EOF_dumpfail
$HOST
Cron backup script failed. Initial tape access was okay, but dump failed.
EOF_dumpfail
exit 1
}
# Success
success () {
$Mail -s "Backup completed successfully - $HOST" $admin << EOF_success
$HOST
Cron backup script was apparently successful. The /etc/dumpdates file is:
`/bin/cat /etc/dumpdates`
EOF_success
}
# Confirm that the tape is in the device
$Rewind || failure
$Dump 0uf $device / || dumpfail
$Dump 0uf $device /usr || dumpfail
$Dump 0uf $device /home || dumpfail
$Dump 0uf $device /var || dumpfail
($Dump 0uf $device /var/spool/mail || dumpfail) && success
$Offline
This script illustrates a number of topics that we've looked at in this document. It starts by setting various parameter values. HOST is set from the output of a command, admin is the administrator of the system, Mt, Dump, and Mail are program names, device is the special device file used to access the tape drive, Rewind and Offline contain the commands to rewind and off-load the tape drive, respectively, using the previously referenced Mt and the necessary options. There are three functions defined: failure, dumpfail, and success. The functions in this script all use a here document to form the contents of the function. We also introduce the logical OR (||) and AND (&&) operators here; each is position between a pair of commands. For the OR operator, the second command will be run only if the first command does not complete successfully. For the AND operator, the second command will be run only if the first command does complete successfully.
The main purpose of the script is done with the Dump commands, i.e. backup the specified file systems. First an attempt is made to rewind the tape. Should this fail, || failure, the failure function is run and we exit the program. If it succeeds we proceed with the backup of each partition in turn, each time checking for successful completion (|| dumpfail). Should it not complete successfully we run the dumpfail subroutine and then exit. If the last backup succeeds we proceed with the success function ((...) && success). Lastly, we rewind the tape and take it offline so that no other user can accidently write over our backup tape.