#!/bin/sh -u # # $0 [strings...] # # A script that chooses cases based on the width of the arguments. # # 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 # The first thing all programs must do is validate their arguments. # # We don't really need to do this check, since the "for" loop will simply # do nothing if no arguments are supplied; but, printing an explicit # message about missing arguments is often helpful to novice Unix users. # if [ "$#" -eq 0 ]; then echo 1>&2 "$0: No arguments; nothing done." exit 1 fi # Loop for each command line argument. # Note that an argument can be present but empty, e.g. ./foo a "" c # for arg do # Echo the argument, so we can see what it is before we process it. # echo "Processing argument '$arg'." # Count the characters in the argument: # Use tr to delete the newline normally added by "echo". # Use tr to delete the blanks normally added by "wc". # Print the number of characters, for verification. # (Most real-world scripts won't do this echo.) # nchars=$(echo "$arg" | tr -d '\n' | wc -c | tr -d ' ') || exit $? echo "DEBUG Argument contains $nchars characters." # Choose cases based on number of chars in each argument. # Remember! First match wins - only one of these cases will execute. # case "$nchars" in 0) echo "Argument contains no characters." ;; [0-9]) echo "Argument contains a 1-9 characters." ;; [0-9][0-9]) echo "Argument contains 10-99 characters." ;; [0-9][0-9][0-9]) echo "Argument contains 100-999 characters." ;; *) echo "Argument $nchars is really, really long!" ;; esac done exit 0