Home‎ > ‎CIS 98‎ > ‎

Lab 8: Decision Constructs

The purpose of this lab is to write scripts that access command line arguments and explore the various uses of decision constructs such as the if statement and its various forms.


If statements start out by testing a command(s) for success or failure. Success is the same as True, and Failure is the same as False. From the command line, enter a test statement that will report whether the following conditions evaluate to success or failure. (Note: I am assuming you are not logged on as root). Use: echo $? to verify your hypothesis.
  • Is the /etc/passwd file readable?
  • Is /dev/null a character device file?
  • Is /etc/password both readable and writable?
  • Is anything in the /etc/motd file?
  • Is /dev/null both readable and writable?
  • Is the IFS variable an empty string? (Try testing $IFS, and then "$IFS")
  • Is the value of the TERM environment variable equal to "xterm"?
  • Is 5 equal to 05?
  • Is 1+2 equal to 3?
  • Is the number of positional paramters greater than or equal to 5 after running the command set `date`?
  • Is the standard input attached to a terminal?
    In the following code samples correct the syntax errors so that the code executes correctly.

    Sample 1:
    if test mkdir /tmp/foo
    then cd /tmp/foo
    else echo "Cannot make directory: /tmp/foo"

    Sample 2:
    if $VAR = 10
    then echo "$VAR" is equal to 10
    else echo "$VAR" is not equal to 10

    Sample 3:
    if test $VAR = HELLO
    else echo Failure

    Sample 4:
    if test $VAR = HELLO
    then echo Success
    else echo Failure

    Sample 5:
    if [ $VAR -eq 25 ]
    then echo "$VAR" is equal to 25
    else echo "$VAR" is not equal to 25

    Programming Assignment

    You will write three simple new scripts, and then modify your ~/lab6/lookup script to be more generalized. 

    Part 1

    A shell script that you write will often require one or more command-line arguments for it to work. If you don't get those arguments, it may be because the user doesn't know what argument(s) to supply. Therefore it is good practice to echo a usage message to stderr that shows the name of your program and whatever arguments it expects. 

        valid - determines if a string represents a valid shell variable name.

    Write a shell program called valid that will take one command line argument and echo onto the screen "yes" if the command line argument is a valid shell variable name, and "no" if the command line argument is not a valid shell variable name. The argument will be a string. Your command should test the string to see if it contains any shell metacharacters that are not allowed in variable names. Hint: use grep.


    1. If the command line does not contain an argument, an appropriate usage message must be displayed.
    2. The command must exit with a 0 exit status if the argument represents a valid variable name, a 1 if the argument is invalid, and 2 if a usage message is generated.
    3. Only one line of output should come from the execution of this script.

    $ valid 
    usage: valid <argument>

    $ valid FOOBAR

    $ valid '$FOOBAR'

    valid 'BAR}{BAR'

    Part 2 

    Shell scripts sometimes need to reprocess input. 

        suffix - renames a file by adding the characters given as the second argument to the end of the name of the file given as the first argument. e.g. suffix memo1 .txt should rename the file memo1 to memo1.txt


    1. If the command line does not contain exactly two arguments, an appropriate usage message must be displayed.
    2. If the filename given as the first argument doesn't exist, and error message should be given
    3. There shoould be no standard output for this program.

    Part 3

    Write a shell script that uses the date command to display the time of day in am or pm notation, (hr:min am/pm), rather than the 24-hour clock time.

        mytime - reports the time in AM or PM notation.


    1. Any command line arguments are ignored.
    2. Only the hour and the minutes should be displayed, no seconds. For example, at the start of class the script would output: 6:00 pm
    3. The command must exit with a 0 exit status.

    Part 4

    Upgrade your lookup script once again.

        lookup - takes a name as an argument and displays four lines of information about the account: user's name and uid, primary group, home directory, and shell.


    1. If no argument is supplied, use the value of $LOGNAME as your input.
    2. generate an error message if the argument is not the name of a valid user.
    3. Exit with a status of 0 if the account information can be displayed; otherwise, exit with a value of 1.


    Create the script files in ~/lab8. Submit the following files:
    1. ~/lab8/valid
    2. ~/lab8/suffix
    3. ~/lab8/mytime
    4. ~/lab8/lookup


    • 5 points per script 
      • 3 for correct execution
      • 2 for style and structure