There are plenty of scripts on your system that use functions as a structured way of handling series of commands. On some Linux systems, for instance, you will find the /etc/rc.d/init.d/functions definition file, which is sourced in all init scripts. Using this method, common tasks such as checking if a process runs, starting or stopping a daemon and so on, only have to be written once, in a general way. If the same task is needed again, the code is recycled. From this functions file the checkpid function:
# Check if $pid (could be plural) are running checkpid() { local i for i in $* ; do [ -d "/proc/$i" ] && return 0 done return 1 } |
This function is reused in the same script in other functions, which are reused in other scripts. The daemon function, for instance, is used in the majority of the startup scripts for starting a server process (on machines that use this system).
This section might be found in your /etc/profile file. The function pathmunge is defined and then used to set the path for the root and other users:
pathmunge () { if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then if [ "$2" = "after" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH fi fi } # Path manipulation if [ `id -u` = 0 ]; then pathmunge /sbin pathmunge /usr/sbin pathmunge /usr/local/sbin fi pathmunge /usr/X11R6/bin after unset pathmunge |
The function takes its first argument to be a path name. If this path name is not yet in the current path, it is added. The second argument to the function defines if the path will be added in front or after the current PATH definition.
Normal users only get /usr/X11R6/bin added to their paths, while root gets a couple of extra directories containing system commands. After being used, the function is unset so that it is not retained.
The following example is one that I use for making backups of the files for my books. It uses SSH keys for enabling the remote connection. Two functions are defined, buplinux and bupbash, that each make a .tar file, which is then compressed and sent to a remote server. After that, the local copy is cleaned up.
On Sunday, only bupbash is executed.
#/bin/bash LOGFILE="/nethome/tille/log/backupscript.log" echo "Starting backups for `date`" >> "$LOGFILE" buplinux() { DIR="/nethome/tille/xml/db/linux-basics/" TAR="Linux.tar" BZIP="$TAR.bz2" SERVER="rincewind" RDIR="/var/www/intra/tille/html/training/" cd "$DIR" tar cf "$TAR" src/*.xml src/images/*.png src/images/*.eps echo "Compressing $TAR..." >> "$LOGFILE" bzip2 "$TAR" echo "...done." >> "$LOGFILE" echo "Copying to $SERVER..." >> "$LOGFILE" scp "$BZIP" "$SERVER:$RDIR" > /dev/null 2>&1 echo "...done." >> "$LOGFILE" echo -e "Done backing up Linux course:\nSource files, PNG and EPS images.\nRubbish removed." >> "$LOGFILE" rm "$BZIP" } bupbash() { DIR="/nethome/tille/xml/db/" TAR="Bash.tar" BZIP="$TAR.bz2" FILES="bash-programming/" SERVER="rincewind" RDIR="/var/www/intra/tille/html/training/" cd "$DIR" tar cf "$TAR" "$FILES" echo "Compressing $TAR..." >> "$LOGFILE" bzip2 "$TAR" echo "...done." >> "$LOGFILE" echo "Copying to $SERVER..." >> "$LOGFILE" scp "$BZIP" "$SERVER:$RDIR" > /dev/null 2>&1 echo "...done." >> "$LOGFILE" echo -e "Done backing up Bash course:\n$FILES\nRubbish removed." >> "$LOGFILE" rm "$BZIP" } DAY=`date +%w` if [ "$DAY" -lt "2" ]; then echo "It is `date +%A`, only backing up Bash course." >> "$LOGFILE" bupbash else buplinux bupbash fi echo -e "Remote backup `date` SUCCESS\n----------" >> "$LOGFILE" |
This script runs from cron, meaning without user interaction, so we redirect standard error from the scp command to /dev/null.
It might be argued that all the separate steps can be combined in a command such as
tar c dir_to_backup/ | bzip2 | ssh server "cat > backup.tar.bz2"
However, if you are interested in intermediate results, which might be recovered upon failure of the script, this is not what you want.
The expression
command &> file
is equivalent to
command > file 2>&1