




















| |
Lab 15
(Chapter 15)
Learning Objectives
- To learn the basic concept
of shell scripting (programming) in Bourne shell
- To learn how shell programs
can be executed
- To learn what shell
variables are and how to use them
- To learn how to pass
command line arguments to a Bourne shell script
- To learn the concept of
command substitution and its use in Bourne shell scripts
- To learn some fundamental
coding principles and their use in Bourne shell scripts
- To practice Bourne shell
scripting by writing a few small size scripts
Lab Work
- Log on to your UNIX system
- Stay in
your home directory and run the set, env, and printenv commands to display
shell variables. In the outputs of these commands, capture below the lines for
the following variables: cwd, gid, group, home, history, path, prompt, shell,
uid, HOME, PATH, TERM, SHELL, MAIL, PWD, USER, GROUP, EDITOR, and PROMPT.
- Add the
following directories in your search path: your current directory and ~/bin.
Show your work.
- What
will be output if the shell script
read_demo in Section
15.3.6 is execute and you give * as input each time you are prompted? Does the
output make sense? Explain.
- Write a
Bourne shell script that takes an ordinary file as an argument and removes the
file if its size is zero. Otherwise, the script displays file’s name, size,
number of hard links, owner, and modify date (in this order) on one line. Your
script must do appropriate exception handling. Show a few sample runs of your
script.
- Write a
Bourne shell script that removes all zero length ordinary files in the
directory passed as an optional argument. If you do not specify the directory
argument, the script uses the present working directory as the default
argument. Do appropriate exception handling in your script. Show a few sample
runs of your script.
- What
happens when you execute the following sequence of shell commands? Give
appropriate explanation where needed.
-
name=date
-
$name
-
`$name`
- Write a
Bourne shell script that displays the following menu and prompts for
one-character input to invoke a menu option, as shown.
- List
all files in the present working directory
-
Display today’s date and time
- Invoke
a C shell script that takes login names of a few users as command line
arguments and displays the pathnames for their home directories.
-
Display whether a file is a ordinary file or a directory
- Create
a backup copy of a file
- Start
a telnet session
- Start
an ftp session
- Exit
Option (c) requires that you ask the user for a list of login
names. For options (d) and (e), prompt the user for file names before invoking a
shell command/program. For options (f) and (g), prompt the user for a domain
name (or IP address) before initiating a telnet or ftp session. The program
should allow the user to try any option any number of times and should quit only
when the user gives option (h) as input. Show a sample run of your script.
A good programming practice is to build code
incrementally—that is, write code for one option, test it, and then go to the
next option.
- Log out.
|