#!/bin/sh -u # # $0 string # # A script that chooses cases based on the first cmd line argument. # # 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. # # This case statement could be an "if" statement. # Note how we do nothing (no command given) if there is one argument. # case "$#" in 1) ;; *) echo 1>&2 "$0: Expecting exactly one argument; found $#" exit 1 ;; esac # Sometimes, echoing the input is a good idea. # Demonstration scripts often do this; real scripts do it less often. # echo "The command line argument was '$1'." # Choose cases based on pattern matching the input. # Remember! First match wins - only one of these cases will execute. # # Since we validated the existence of a command line argument, we know # that $1 has a value (though it could be an empty string). # # Style: If the pattern of the case fits before the first tab stop, # you can put the rest of the case (the commands) on the same # line after a tab. If it doesn't, start the commands on a new # line, indented one tab stop. # case "$1" in "") echo "Your input is empty (no characters)." ;; *" "*) echo "Your input contains blanks." ;; [0-9][0-9]*) # The pattern in this case is wider than one tab stop, # so I put the command on a new line for readability. echo "Your input starts with two digits." ;; [0-9]*) echo "Your input starts with only one digit." ;; *[0-9]) echo "Your input ends with a digit." ;; .*) echo "Your input starts with a dot." ;; ?|??|???|????) # The pattern in this case is wider than one tab stop, # so I put the commands on a new line for readability. # Use tr to delete the newline normally added by "echo". # Check the return code of the wc in case of error. # echo "Your input is between one and four characters long." width=$(echo "$1" | tr -d '\n' | wc -c) || exit $? echo "In fact, it is exactly $width characters long." ;; *) echo "*: (DEFAULT) You typed '$1'." echo "This is the default case." echo "The * pattern always matches." ;; esac exit 0