CS160B Assignment 2

In this assignment you will write a single shell script chk.sh to output certain kinds of information about two different types of Unix objects: files and users. This will require processing of command-line arguments and the use of complex, nested conditional statements (if-then-else) to control program flow.

Writing Your Script

This shell script, chk.sh, can be used to check a file if it is invoked as:

chk.sh -f filepath

or to check a user if it is invoked as

chk.sh -u user

Here is the information it should report:

1) Running chk.sh -f filepath accomplishes:

If filepath does not exist (and is not a symbolic link), your program should report this in an informative error message. In this case, you should exit with an error.

2) Running chk.sh -u user accomplishes:

Note: you may not use the finger or pinky programs to get your information for chk.sh -u

If the user exists on the system, report

If the user doesn't exist, report an informative error message, and exit with an error.

You may not use the finger or pinky programs to get your information for chk.sh -u

Your program must, of course, check its arguments well and report any inconsistencies. If your program detects an error it should give the user sufficient information to diagnose the error and rerun the program successfully. It should also set the exit status appropriately.

This program will be much easier if you think and map out its steps before you start coding!

Hand in (1) a copy of your shell script and (2) an output script session with the results of the following commands:

User names may be very similar. Your program should be careful in how it obtains information so as to get only the information for the user name it wants. The purpose of the large number of test cases above is to discover bugs that occur with unexpected input. You may have to do some work after running your test cases, so start your testing early!

Turning In Your Assignment

Hint: Functions to help work on your Assignment 2

Functions are a useful tool for writing scripts. Greg Boyd provides two functions here that are very simple and easy to use. The purpose of these functions are to help you handle errors when your program diagnoses them and to allow you to output error messages in a standard format that can be changed in a central place. In addition these functions write their error messages to standard error, another technique that we will cover later.

The first function, named error outputs the error message you pass it to standard error in a standard format. The second function, named fatal, simply calls the error function with the error message you give it, then exits the program with an exit status to indicate failure.

You may customize the contents of the functions to output the kind of information you want. Function are called as if they are commands. The following calls to the function error generate the same error message:

you will get a slightly nicer error message with the form

error "File '$file' does not exist"

Each invocation of the error function above will output the error message and continue your program. If you wanted to exit the program instead, simply replace the function name with fatal

Helper file below:

# this file contains two function error() and fatal() as
# described below.

error () {
    # error: output an error message and return
    # the error messages contain the program name
    # and are written to standard error

    local prog=$(basename $0)
    echo -e "$prog:ERROR: $*">&2

fatal() {
    # fatal: output an error message and then exit
    # the error() function is used.
    error $*
    # if you want to output a message about how to successfully
    # run your program (called a 'usage message'), this would
    # be the place to do it

    # now exit with an error
    exit 1

# your program goes here