Chapter 5 - The Bourne Shell
Quiz
1. Who wrote the Bourne shell?
- Stephen Bourne wrote the Bourne shell.
2. Describe a common use of the built-in variable $$.
- The built-in variable $$ contains the current process ID. In shell
scripts, this is often used, appended to a filename, to generate a temporary
filename that should be unique.
3. What is the easiest way to reexecute your ì.profileî file?
- The easiest way to reexecute your .profile file is to use the shellís
ìdotî command. To do this, type ì. .profileî.
4. What is the method used to have a shell variable defined in a subshell
(i.e., to pass the value to the subshell)?
- Use the export built-in command to maintain a shell variable's
value in a subshell.
5. What debugging features does the Bourne shell provide?
- The Bourne shell provides two arguments, -v and -x,
which cause the shell to print out debugging information as it executes
a script or commands.
Exercises
5.1 Write a shhelp utility ... [level: easy]
- This is patterned after the man commandís ability to list
one line summaries for man pages containing specified keywords. The
first thing you have to do is build a file containing one line
descriptions of all the built-in commands in the Bourne shell. You can
find the commands described in the man page for sh. Then use
grep to get the proper line out of the file for the keyword specified
on the command line. Be careful to make your grep command specific
enough so that multiple commands arenít found (for example, if the user
types ìshhelp ñk exî you want the result to be ìno such commandî rather
than the summary lines for both exec and exit).
5.2 Write a utility called junk ... [level: medium]
- The requirements for this script are fairly straightforward. The
script should process the arguments from the command line to find the
files to be junked or determine what function (list or purge) will be
performed. In the case of files to be junked, move them into the junk
directory rather than removing them.
5.3 Modify the junk script so that it is menu driven. [level: easy]
- In this case, list the possible functions (junk, purge, and list).
In the case of junking files, a secondary menu will be necessary where
the user can select which files are to be junked.
Projects
1. Write a crafty script called ghoul that is difficult to kill; when it
receives a SIGINT (from a Control-C), it should create a copy of
itself before dying. Thus, every time an unwary user tries to kill a ghoul,
another ghoul is created to take its place! Of course, ghoul can still be
killed by a SIGKILL (-9) signal. [level: medium]
- This script will use the trap built-in command to trap a SIGINT
signal and start up a new copy of itself before exiting.
2. Build a phone book utility that allows you to access and modify an alphabetical
list of names, addresses, and telephone numbers. Use the utilities described
in Chapter 3, such as awk and sed, to maintain and edit the file of phone
book information. [level: hard]
- Some of the things learned from the shhelp exercise above
may help in designing this program. In general, new lines could be added
by appending new information to the end of the file and resorting the
file. Old information can be deleted using grep with the -v
argument and updating the file with the result. Edits can be done with
sed or could be done with a combination of insert and delete
functions.
3. Build a process management utility that allows you to kill processes
on the basis of their CPU usage, user ID, total elapsed time, and so forth.
This kind of utility would be especially useful to system administrators.
(See Chapter 15.) [level: hard]
- The hardest part of this project is designing the command interface.
Once that is accomplished, you merely need to take the output from the
ps command and parse it for the specified information (get user
IDs, elapsed time, CPU usage, or whatever other attribute you would
like to include) and generate a list of the processes that pass the
test. Then parse out the process IDs (probably with awk) to be
used in a kill command. One way to put the PIDs into a shell
variable would be to parse out the selected IDs within grave accents
(which executes a shell command within a shell command).
|