




















| |
Lab 17
(Chapter 17)
Learning Objectives
- To learn the basic concept
of shell scripting (programming) in C 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 C shell script
- To learn the concept of
command substitution and its use in C shell scripts
- To learn some fundamental
coding principles and their use in C shell scripts
- To practice C 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
keyin_demo in Section
17.3.5 is execute and you give * as input each time you are prompted? Does the
output make sense? Explain.
- Write a
C 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
C 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.
a.
set name=date
b.
$name
c.
`$name`
- Write a
C shell script that displays the following menu and prompts for one-character
input to invoke a menu option, as shown.
1. List all
files in the present working directory
2. Display
today’s date and time
3. 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.
4. Display
whether a file is a ordinary file or a directory
5. Create a
backup copy of a file
6. Start a
telnet session
7. Start an ftp
session
8. 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.
|