#!/bin/bash -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. #------------------------------------------------------------- # Student Name: Ian! D. Allen # Algonquin EMail Address: alleni # Student Number: 000-000-000 # Course Number: CST8129 # Lab Section Number: 010 # Professor Name: Ian! D. Allen # Assignment Name/Number/Date: Exercise #6, script 4, due October 22 # Comment: This is a sample assignment label. # see http://idallen.com/teaching/assignment_standards.html #------------------------------------------------------------- # Set standard search path and umask. # PATH=/bin:/usr/bin ; export PATH 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 arguments back to the user before using them. # echo "Your pathname is: $*" # 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 file or directory. # echo 1>&2 "$0: argument '$1' is not a file or 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