CS 232 Programming Project: Bourne Shell Scripts


Overview.

There are situations in which it would be useful to be able to determine which of the workstations in the UNIX lab are occupied, and which are vacant. For example, suppose a person at home or in their dorm room wants to test their OS project, which might crash the machine on which they run it. Applying the Golden Rule, such a project should only be tested on a machine that is unoccupied.

A user who is logged into a machine can use the who command to determine what users are logged in on that machine. However, the process of repeatedly logging in and running who on various machines until an empty one is found is tedious. Also, some of the machines may be booted into Windows, or shut down, further complicating the problem. This project is to write a shell script that automates this process.

Details

One feature that distinquished UNIX from its predecessor operating systems was its provision of the shell, which provides much of the UNIX user-interface, plus a built-in shell command language. Over the years, various shells have been created for UNIX, including:

and others. Different shells have different features and conventions, including different command-languages.

sh is the original UNIX shell, and is called the bourne shell after Steve Bourne, its creator. While later shells such as csh, ksh, tcsh, and bash extended the features of the bourne shell, it is still available on every UNIX system. Bourne shell scripts are thus very portable across different UNIX platforms.

To create a shell script, you simply store a sequence of shell commands in a file, and then make this file executable (using the system chmod command). Typing the name of the file then executes the commands within it.

The commands that can appear in a shell script include both

  1. system commands (e.g., ls, ps, cd, ...); and
  2. shell commands, which are unique to a given shell.
The shell commands for a given shell are specified on its manual page (e.g., type man sh to read about the bourne shell commands, man csh to read about the c-shell, etc.). Such commands include selection commands (e.g., if, case), repetition commands (e.g., for, while, until), and other commands useful for problem-solving.

For safety, shell scripts should be executed in a shell that is distinct from the login shell. For the bourne shell, this is accomplished by making

   #!/bin/sh
the first line of the shell script. The effect of this line is to make the system
  1. execute the bourne shell (as a separate process), and
  2. pass the script to that shell to be executed.
This has a beneficial side-effect of indicating to a reader the particular shell language the script is written in.

Your assignment is to write a bourne shell script that when executed, prints out a list of the Ulab workstations, indicating those that are booted into Linux and are unoccupied, those that are are booted into Linux but are occupied, those that are booted into Windows, those that are shut down, and any other information you think might be useful.

You can find tutorials for bourne shell scripting on the web -- use your favorite search engine to find them, if necessary.

Shell-script comments begin with the # symbol and end at the end of a line. Your script should include full documentation and should use white space (esp. indentation) to reflect its structure.

Plan of Action

  1. Spend a few days studying the bourne shell command language, using the sh manual page and/or WWW tutorials.
  2. Using that information, write a prototype script that uses the echo command to display the name of each workstation in the UNIX lab on a separate line. You will have to walk around the lab writing down the names of the machines. Put those names into a file, one per line.
  3. Find a way to determine how many users are logged into a workstation.
  4. Extend your prototype script with the command(s) to find out how many users are logged into a given workstation without logging in yourself -- although the script may "log in".
  5. Find a way to capture the output from a command, and add this to your script.
  6. Find a way to only display a given workstation's name if the output captured in the previous step indicates that no users are logged into that workstation.
  7. When you can do each of the preceding steps, you should have a script that solves the problem for systems that are booted into Linux. Devise a means that your script can use to distinguish workstations running Linux from those running Windows, that are turned off, and so on. Incorporate that mechanism into your script.
  8. Test your script thoroughly before turning it in.
You may discuss with your classmates broad ideas and basic approaches on how to solve the different part of this problem. However, you may not look at anyone else's script.

Your script should be reasonably efficient, and should "clean up" after itself as necessary, so as to leave the system in the same state as it was initially.

Note that your script should not be a long program. (Mine is less than 20 lines long, not counting documentation.) It is amazing how powerful a short shell script can be.

However, if you have not written a bourne shell script before, then this may be a long project, because you will have a great deal of knowledge to acquire. Getting the bourne shell command-language syntax right is tricky, which may take some trial and error... Don't procrastinate!

As with a normal program, your script is to be carefully documented, and written with attention to readability by someone other than yourself. Make your code hospitable!

Turn in: Use the program script to create a file in which you

  1. Execute your shell script to demonstrate that it satisfies the project requirements.

Submit your code to /home/cs/232/current/<yourid>/proj2/, along with the script file, and this grade sheet, which you have altered by putting your name in.

Due date: Monday, Feb. 27, 11:59 p.m. (i.e., one minute before midnight)


CS > 232 > Projects > 2


This page maintained by Victor Norman.