#!/bin/sh -u # Demonstrate prompting for missing command line arguments. # Syntax: # $0 [ permissions [ pathname ] ] # Purpose: # Apply the permissions to the pathname, only if the pathname is # writable by the script user. Prompt for missing command arguments. # -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 # The user might enter any number of arguments. # Handle all the cases using a series of IF/ELSE tests: # if [ $# -gt 2 ]; then echo 1>&2 "$0: Expecting permissions and pathname; found $# args ($*)" exit 1 fi if [ $# -eq 0 ]; then echo 1>&2 "Enter permissions and pathname:" read permissions pathname || exit $? else if [ $# -eq 1 ]; then permissions="$1" # first arg is permissions echo 1>&2 "Enter pathname:" read pathname || exit $? else # we must have exactly two arguments on the command line # permissions="$1" # first arg is permissions pathname="$2" # second arg is pathname fi fi # See if either string is empty (nothing entered). # if [ -z "$permissions" -o -z "$pathname" ] ; then echo 1>&2 "$0: empty permissions or pathname; nothing done" exit 2 fi if [ ! -e "$pathname" ] ; then echo 1>&2 "$0: pathname '$pathname' does not exist; nothing done" exit 3 fi if [ ! -w "$pathname" ] ; then echo 1>&2 "$0: pathname '$pathname' is not writable; nothing done" exit 4 fi if chmod "$permissions" "$pathname" ; then echo "Permissions changed to '$permissions' on '$pathname'" else echo 1>&2 "$0: chmod '$permissions' '$pathname' failed with status $?" exit 5 fi