#!/bin/sh -u # Show the equivalence of nested IF statements and ELIF # Syntax: # $0 pathname # Purpose: # Process the pathname and tell some things about it. # Do different things for files and directories. # -IAN! idallen@ncf.ca # Set the search path for the shell to be the standard places. # Set the umask to be non-restrictive and friendly to others. # PATH=/bin:/usr/bin ; export PATH umask 022 # Make sure this script is called with exactly one argument. # if [ $# -ne 1 ] ; then echo 1>&2 "$0: Expecting one pathname argument; found $# ($*)" exit 1 fi # Code using nested IF statements requires two levels of indentation: # if [ -f "$1" ] ; then echo "Argument '$1' is a file name." if [ -s "$1" ]; then not='not ' else not='' fi echo "The file is ${not}empty." else if [ -d "$1" ] ; then echo "Argument '$1' is a directory name." if [ -w "$1" ]; then not='' else not='not ' fi echo "The directory is ${not}writable by you." else echo "Argument '$1' is not an accessible directory or file name." fi fi # Code using ELIF statements requires less indentation: # if [ -f "$1" ] ; then echo "Argument '$1' is a file name." if [ -s "$1" ]; then not='not ' else not='' fi echo "The file is ${not}empty." elif [ -d "$1" ] ; then echo "Argument '$1' is a directory name." if [ -w "$1" ]; then not='' else not='not ' fi echo "The directory is ${not}writable by you." else echo "Argument '$1' is not an accessible directory or file name." fi