#!/bin/sh -u # Demonstrate method 2 for 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. # # Comments in this script show the Input/Validation/Processing # phases of the script. #------------------------------------------------------------------ # -IAN! idallen@idallen.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 #------------------------------------------------------------- # Input section. # Exit the script if more than two arguments are supplied. # if [ $# -gt 2 ]; then echo 1>&2 "$0: Expecting permissions and pathname; found $# args ($*)" exit 1 fi # The user might enter 0, 1, or 2 arguments. # Handle all the cases using a series of IF/ELSE tests. # This version reads permissions and then pathname if both are missing. # if [ $# -lt 1 ]; then echo 1>&2 "Enter permissions:" read permissions || exit $? else permissions="$1" # first arg is permissions fi if [ $# -lt 2 ]; then echo 1>&2 "Enter pathname:" read pathname || exit $? else pathname="$2" # second arg is pathname fi # #------------------------------------------------------------- #------------------------------------------------------------- # Argument validation section. # # Exit if either string is empty (nothing entered). # Exit if the pathame does not exist or is not writable. # 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 # #------------------------------------------------------------- #------------------------------------------------------------- # Processing section. # We have validated the arguments before doing any processing. # 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