#!/bin/sh -u # Validate a single command line argument (file or directory). #------------------------------------------------------------ # Syntax: # $0 pathname #------------------------------------------------------------ # Purpose: # If the pathname is a file, make sure it is readable, writable, # and non-zero size. # If the pathname is a directory, make sure it is readable and # searchable. # Otherwise, print an error message and exit non-zero. #------------------------------------------------------------- # -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 we have exactly one argument. # If not, echo the count and incorrect input back to the user on stderr. # if [ $# -ne 1 ] ; then echo 1>&2 "$0: Expecting 1 pathname argument, found $# ($*)" exit 1 fi # Optional: Echo the user's input argument back to the user before using it. # echo "Your pathname is: $1" # Check for file or directory; do appropriate tests for each. # if [ -f "$1" ] ; then if [ ! -r "$1" ] ; then echo 1>&2 "$0: file '$1' is not readable." exit 1 fi if [ ! -w "$1" ] ; then echo 1>&2 "$0: file '$1' is not writable." exit 1 fi if [ ! -s "$1" ] ; then echo 1>&2 "$0: file '$1' is empty." exit 1 fi elif [ -d "$1" ] ; then if [ ! -r "$1" ] ; then echo 1>&2 "$0: directory '$1' is not readable." exit 1 fi if [ ! -x "$1" ] ; then echo 1>&2 "$0: directory '$1' is not searchable." exit 1 fi else # If we get here, it is not a plain file or a directory. # (It might be a special file such as /dev/null or /dev/zero) # echo 1>&2 "$0: argument '$1' is not a plain file or a directory." exit 1 fi # If we get here, it must have passed all the tests. # Optional: Print a confirmation message. (not required) # echo "All tests passed." exit 0