#!/bin/bash # This is a version of the script you are to write in # Exercise 1 of Module 4. # # Comments with numbers in square brackets, e.g. [1], # begin sections of the script with the number indicating # roughly the order in which you were asked to write # that section. # # David Harrison - May 2002 # [4], [5] # The name of the program. PROGNAME=`basename $0` # [8] # This is the command to be executed. May be modified # below. LS='/bin/ls' # [9], [10], [11] # Process optional -l or -m flag case $1 in # We could set, for example: -l) LS="$LS -l" # LS='/bin/ls -l' shift;; # and similarly for the -m flag -m) LS="$LS -m" # but this way I only specify the shift;; # location of /bin/ls in one place. esac # This is good coding practice. # [1] # If no arguments, just execute $LS and exit. if [ $# -eq 0 ] then $LS exit fi # [2] # We have arguments. Assemble them into $FILENAMES FILENAMES='' while [ $# -gt 0 ] do # [3], [6] # If no such file, issue a message and exit # with $? = 1 if [ ! -f $1 ] then # [7] # Direct echo's output to stderr. echo "$PROGNAME: $1: No such file found" 1>&2 exit 1 fi FILENAMES="$FILENAMES $1" shift done $LS $FILENAMES