Home‎ > ‎CIS 98‎ > ‎

Lab 7: Decision Constructs

The purpose of this lab is to explore the various uses of decision constructs such as the if and case statements. 


  1. In the following code sample correct the syntax errors so that the code executes correctly.
    • case "$VAR"
          red) echo '$VAR is red'
          green) echo '$VAR is green'
          blue) echo '$VAR is blue'
      esac

Programming Assignment

  1. cecho - a color version of the echo command.
    The purpose of this program is to correctly use the case statement to write a shell script that prints its arguments in the same way as the echo command, but in a specified color.
    Requirements:
    1. Your program must be named cecho, executable, and use the bash shell as its interpreter.
    2. The program must be able to handle any number of arguments, the first one of which may specify a color.   cecho -[r|g|b] ...
    3. If the first argument is a valid color option, your command should echo out all the other arguments in that color. If the first argument is not a valid color, your command should function exactly like the echo command.
    4. After your command is done, the user's terminal screen colors should be the same as before executing your command.
    5. The strings for changing the foreground color are provided in a library called colors. You may source this library using an absolute path.
    6. Use a case statement as the backbone of this program.


  2. Extra Credit
    Use the date and who commands to write a program called conntime that prints the number of hours and minutes that a user has been logged on to the system. (You can assume that this time will be less than 24 hours).
    If no argument is provided on the command line, compute the time for the person who is running the program. If a username is specified on the command line, compute the time for that user, or output an error message if that user is not logged on.

To Submit

When you are done there should be one or two programs: cecho and optionally conntime in your lab7 directory.

Grading Rubric

9-10 points -
Your program is executable and produces appropriate usage messages to stderr.
7-8 points -
Your script produces correct output
5-6 points -
Your script does not run to completion for any reason.
less than 5 pts -
Your assignment is late.
Comments