UNIX Shell Script - Spy

Due: Thursday, April 25, 2002 at 11:59 pm

For this assignment, you are to write a Bourne shell program called spy which will give let you know what certain users are currently doing on the system. The program will take as input a list of login ID's or a filename that will contain the list of login ID's.

The format of the command will be as follows:

So the user can type in any number of login ID's, or give the -f argument and a filename. The file would contain a list of login ID's (one name per line). The output of the command is to contain the login ID and what that user is currently doing on the system.

The output must be sorted alphabetically by the login ID name. Also if a user in on the logged on more than once, they should appear once in the list for each time they are logged on. If a user is not logged on, they should not appear in the output. If a login ID is given more than once, the output must only have the information for that user listed once for each time the user is logged on. Your shell script must check that it is being used correctly. If the -f option option is given, there can only be one more argument. This additional argument MUST be the name of a file. See the below examples for other errors for which you must check. Also, when an error is encountered, an error message must be printed along with a statement of proper usage.

Here are some examples of its use. Assume the file "namelist" contains the login ID's: moher, becker and drake. It is also assumed that moher is not currently logged on.

	[ernie] ~>spy troy reed moher 
	reed     pine
	troy     gcc helloworld.c
	troy     vi helloworld.c

	[ernie] ~>spy -f namelist 
        becker   csh 
	drake    emacs calender.st

	[ernie] ~>spy troy drake reed drake 
	drake    emacs calender.st
	reed     pine
	troy     gcc helloworld.c
	troy     vi helloworld.c

	[ernie] ~>spy -f nofile
        spy: nofile: Cannot access: No such file
        Usage: spy -f filename | spy name1 name2 ...

	[ernie] ~>spy -f WWW
        spy: WWW: Cannot access: No a file
        Usage: spy -f filename | spy name1 name2 ...

	[ernie] ~>spy 
        spy: arguments required for usage
        Usage: spy -f filename | spy name1 name2 ...

	[ernie] ~>spy -f
        spy: one file is required for option -f
        Usage: spy -f filename | spy name1 name2 ...

	[ernie] ~>spy -f namelist troy
        spy: one file is required for option -f
        Usage: spy -f filename | spy name1 name2 ...

	[ernie] ~>spy -l troy
        spy: illegal option -l
        Usage: spy -f filename | spy name1 name2 ...

In the first, second and third examples, the shell script ran correctly. The remaining examples had some usage errors. In the fourth and fifth examples, the listed item was not a file. In the sixth example, no arguments were given. In the seventh and eighth example, the -f option was given and either no items or more than one item was listed after the option. In the nineth example, an illegal option was given.

Once you get the basics of your program running, you may wish to try your program when you are logged onto ernie, oscar or grover, instead of being logged on in the EECS Department Lab. The following commands will be very useful:

Note that not all of these commands are discussed in the Sobell book. You can find out information about any UNIX command by using the man command.

You may also find that you will need to create a temporary file in order to complete this assignment. All temporary files must be stored in the /tmp directory, have a unique file name and be removed prior to the completion of the script program. Do not use read in your assignment. Make sure your code is written using good programming style. This includes the use of comments, indentation, blank lines and meaningful identifier names. At the top of your file you must have a comment explaining the purpose of the file including your name, and lab section day and time.

As always, late assignments will not be accepted.

You are to submit this assignment to the digital drop box by the time it is due.