#!/bin/sh # mostly if_demo3 from Sarwar, Koretsky, Sarwar (2001), pp. 424-6 # modified to show both test and [ ] forms of if statements if test $# -eq 0 # basic form of test then echo "Usage: $0 file" exit 1 elif test $# -gt 1 then echo "Usage: $0 file" exit 1 else ls "$1" 2> /dev/null 1>&2 # redirects stderr to black hole, and # then redirects stdout to stderr, so # only effect is: set $? - the err flag # depending on file's existence if test $? -ne 0 then echo "$1: not found in the present working directory." exit 1 fi if [ -f "$1" ] # switched to short form of test then filename="$1" set `ls -il $filename` # reset command line args to file list inode="$1" size="$6" echo "Name : Inode : Size" echo "$filename : $inode : $size" exit 0 elif [ -d "$1" ] then nfiles=`ls "$1" | wc -w` echo "The number of files in the directory is $nfiles" exit 0 else echo "$0: argument must be an ordinary file" fi fi