DistributedNetworks DistributedNetworks

Shell Variables   «Prev  Next»

Using Shell Scripting Array Variables - Exercise

Using array variables

Reviewing how variables are used

Objective: Explain variable usage in a simple script.

Exercise scoring

This exercise is worth 6 points.


Consider the following expanded version of a script used in one of the MouseOver applets in this module.

# Two-step database file conversion script 
if [ $# <> 2 ];  then
echo Error: Please include 1) a file to convert 
echo and 2) a username on the command line. 
echo (e.g. switch hotelreg.dat thomasa)
echo Contact the sys admin for assistance
if [ -f $1 ]; then
if [ ‘grep -c $2 /etc/passwd‘ ]; then
convertdb $1 -user $2 > tmp.$$
filterdb tmp.$$ > $1

Now answer the following questions about the variables used in this script.
  1. What is the first if statement testing to ascertain?
  2. The second if statement tests for the existence of a file. What file's existence is being ascertained?
  3. The third if statement uses the grep command to search for a string. What string is being searched for?
  4. Why is $$ used to create a unique filename?
  5. On the line with filterdb, what does the $1 represent? Is it the same as the $1 in the second if statement?

Submitting your exercise

Submit your answer in the text box below.