#!/bin/sh -u # # $0 pathname # # A script that identifies if the pathname given on the command line # is a file, is non-empty, is readable, and is writable. # Nonexistent and non-files are silently ignored. # # WARNING: Many of the comments in this file are "Instructor-Type" # comments and are *not* appropriate for real scripts. Do not put # "Instructor-Type" comments into the scripts that you submit for marking. # I put them in my example files because I am teaching you how to # write scripts; do not submit my teaching comments back to me again. # Read the week7.txt notes for more details on good comment style. # # -IAN! idallen@ncf.ca June 2001 # All scripts must start with lines similar to these two: # export PATH=/bin:/usr/bin umask 022 # Validate the script arguments. # Output what the extra arguments were, to help the user. # if [ "$#" -ne 1 ]; then echo 1>&2 "$0: Expecting exactly one argument; found $# ($*)" exit 1 fi # Test four conditions in four nested 'if' statements. # (You can also combine these into a single statement; but, it doesn't # read any better and isn't significantly faster.) # Pathnames failing any of the tests are silently ignored. # # With this much indentation, the "echo" message runs off the right edge # of the screen in the source file here unless we put it in a variable # and use that in the "echo" command: # message="File '$1' is not empty, is readable, and is writable" if [ -f "$1" ]; then if [ -s "$1" ]; then if [ -r "$1" ]; then if [ -w "$1" ]; then echo "$message" fi fi fi fi # Test the same four conditions using four non-nested 'if' statements. # This has the same effect; but, much less indentation is needed. # We exit the script if any of the four conditions is *NOT* true. # If we make it to the end of the script, all the conditions must be true. # if [ ! -f "$1" ]; then exit 1 # is not a file fi if [ ! -s "$1" ]; then exit 1 # has no size (is empty) fi if [ ! -r "$1" ]; then exit 1 # is not readable fi if [ ! -w "$1" ]; then exit 1 # is not writable fi echo "$message" # it passed all the tests! exit 0