#!/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@idallen.ca # Use standard search path, friendly umask, ASCII collating and sorting. # Set the language and character set to be ASCII/C standard. # PATH=/bin:/usr/bin ; export PATH LC_COLLATE=C ; export LC_COLLATE LANG=C ; export LANG 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 # this script exits with the return code of the last command executed