Previous | Table of Contents | Next |
In this chapter I covered using shell scripts to solve two problems:
In the first example, I showed you how to move a directory between file systems using the tar command. This example also showed you how to use the basename and dirname commands to extract parts of a path for your use.
In the second example, you developed three scripts that you used to modify and view the contents of an address book. Some of the highlights of these scripts are:
The examples in this chapter demonstrate how you can apply the tools that you have covered in previous chapters to solve real problems. Using these scripts as examples, you can see some of the techniques used to solve everyday problems.
In the next chapter I will show you how to make sure the scripts you write are portable between different versions of UNIX.
40 if [ -d "$2" ] ; then 41 42 DESTDIR='( cd "$2" ; pwd ;)' 43 44 else 45 46 # if the destination doesn't exist then 47 # assume the destination is the new name 48 # for the directory 49 DESTDIR="'/usr/bin/dirname $2'" 50 NEWNAME="'/bin/basename $2'" 51 52 # if dirname returns a relative dir we will 53 # be confused after cd'ing later on. So 54 # reset it to the full path. 55 DESTDIR='(cd $DESTDIR ; pwd ;)' 56 57 # if the parent of the destination doesn't 58 # exist, we're in trouble. Tell the user 59 # and exit. 60 if [ ! -d "$DESTDIR" ] ; then 61 printERROR "A parent of the destination ⇒directory $2 does not exist" 62 fi 63 64 fi 65
PATH=/bin:/usr/bin # check that a name is given if [ $# -lt 1 ] ; then printUSAGE "'basename $0' name" exit 1 fi # check that the address book exists MYADDRESSBOOK="$HOME/addressbook" if [ ! -f "$MYADDRESSBOOK" ] ; then printERROR "$MYADDESSBOOK does not exists, or is ⇒not a file." exit 1 fi
awk -F: '{ printf "%-10s %s\n%-10s %s\n%-10s %s\n%-10s ⇒%s\n\n",\ "Name:",$1,"Email:",$2,"Address:",$3, ⇒"Phone:",$4 ; }'
$ ./delperson.01 to Name: James T. Kirk Email: jim@enterprise.mil Address: 1701 Main Street Anytown Iowa Phone: 555-555-5555 Delete this entry (y/n)? [n]
Previous | Table of Contents | Next |