Chapter 6 - The Korn Shell
Quiz
1. Who wrote the Korn shell?
- David Korn wrote the Korn shell.
2. Why is the alias mechanism useful?
- The alias mechanism provides an easy way to modify the usage of a
standard UNIX command or to rename a command to something you prefer
without having to write and store a small shell script. The most common
use is to add arguments to a command that you always use but do not
want to type each time you use the command (e.g. alias ls=íls ñsFí
).
3. How can you reedit and reexecute previous commands?
- You can re-edit and reexecute commands using the built-in command
fc.
4. Does the Korn shell support recursive functions?
- Yes.
5. Describe the modern syntax of the test command.
- The modern syntax of test allows you to use ì[[î and ì]]î
to enclose a test rather than using the test command.
Exercises
6.1 Rewrite the junk script of this chapter so that it is menu driven. Use
the select command. [level: easy]
- You probably used a series of if/fi blocks to check each menu
item in your Bourne shell version of junk. The Korn shell built-in select
will make the code easier to follow.
6.2 Write a function called dateToDays that takes three parametersóa month
string such as Sep, a day number such as 18, and a year number such as 1962óand
returns the number of days from January 1, 1900, to the date. [level: medium]
- The hard part of this is knowing how many days are in each month
and handling leap years. One way to do this might be to use the UNIX
cal command and wc with the -w argument to find
how many days are in a particular month.
6.3 Write a set of functions that emulate the directory stack facilities
of the C shell (described in the next chapter). Use environment variables
to hold the stack and its size. [level: medium]
- A shell array with an index value telling you what the ìcurrentî
location is will handle this.
6.4 Build a script called pulse that takes two parameters: the name
of a script and an integer. pulse should execute the specified
script for the specified number of seconds, suspend the script for the same
number of seconds, and continue this cycle until the script is finished.
[level: hard]
- A loop utilizing sleep and kill with the proper values
will do most of the work here. Start the target script and then enter
a loop which sleeps for the specified amount of time and then alternatively
sends a suspend or wake up signal.
Projects
1. Write a skeleton script that allows system administration tasks to be
performed automatically from a menu driven interface. Useful tasks to automate
include the following:
automatic deletion of core files
automatic warnings to those who use a lot of CPU time or disk space
automatic archiving
Donít worry about making the tasks do anything just yet; weíll fill that
in at the end of Chapter 15. For now, concentrate on making the menu and
task selection work properly, and just use the echo or print
commands to print out what would happen for each selection. [level: easy]
- Again, the hard part is actually building the functionality required.
The menu that wraps around it isnít very difficult. Use the find
command to find all core files in a file system. Use ps to examine
CPU usage. Use the du command to determine the amount of disk
space used by a particular userís home directory (you will need to be
running as superuser to read another userís home directory, although
you can test your script on your own directory). Use the find
command to find files that are older than a certain amount of time that
are candidates for archival.
2. Write an alias manager script that allows you to choose DOS emulation,
VMS emulation, or no emulation. [level: medium]
- Youíll need a list of aliases for each type of emulation (for example,
a ìdirî alias which executes ls for DOS and VMS). Once you have
a set of aliases for each type of emulation, you merely have to parse
an argument describing the emulation type and execute the proper block
of alias commands.
|