#!/bin/sh -u # Demonstrate method 1 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 have entered 0, 1, or 2 arguments. # Handle all the cases using a series of IF/ELSE tests. # This version reads both permissions and pathname if both are missing. # 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 # #------------------------------------------------------------- #------------------------------------------------------------- # 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