To remedy this problem, you can substitute ${ 1-mover} for the word "mover". Now, if no com-mand line argument is supplied to compile, ${1-mover} is replaced by the word "mover":
cc -0 $ {l-mover } $ {l-mover }.c ${l-mover}l.o ${1-mover}2.0 $ (l-mover }3.0
Now, when you type
$ compile
the command
cc -0 mover mover.c moverl.o mover2.0 mover3.0
is executed. However, if you type
$ compile lister
the command
cc -0 lister lister.c listerl.o lister2.0 lister3.0
is executed. In the following discussion of conditional and repetitive program statements, you'll see ways to make the compile program even more versatile.
Structured Statements
The shell allows you to use the following conditional, repetitive, and data input program statements:
• conditional statements: case and if statements
• repetitive statements: for, while, and until statements
• data input statements: the read statement
A conditional statement executes no more than one of its component statements, based on the results of a conditional test. The shell's conditional constructs are IF and CASE.
A repetitive statement specifies that a list of commands in the body of the statement may be executed repeatedly. The shell's repetitive constructs are FOR, WHILE, and UNTIL.
The following pages describe each of these constructs, and shows ways to use them in shell programs.
IF Statements
An if statement may be in one of the following formats:
(1 ) if command-listl then command-list2 fi
(2 ) i f command-listl then command-list2 else command-list3 fi
Shell Programming-8560 Series System Users Writing Shell Programs
In each of the above formats, command-list represents a list of one or more commands. if the last command in the command-list following the if statement returns a true value (zero exit status), the
$? shell variable is set to zero and the command-list following the then statement is executed.
Otherwise, the command-list following the else statement (if there is an else statement) is exe-cuted. The reserved word fi terminates the if statement.
As formats 3 and 4 demonstrate, the command-list can include additional if statements of the type shown in formats 1 and 2.
Format 5 is equivalent to format 4.
A final note regarding the if construction. The sequence
Writing Shell Programs
Structured Statements Shell Programming-8560 Series System Users
Conversely, the sequence if command-listl then true
else command-list2 fi
may be written
command-listl :: (command-list2)
In each case, the exit status of the last simple command executed in command-list1 determines whether or not command-list2 is executed.
The TRUE and FALSE Commands. The true command returns a true value {zero exit status};
the false command returns a false value {non-zero exit status}. Thus, the statement if true
then echo Hello!
else echo Goodbye!
fi
always executes the echo Hello! command, whereas the statement if false
then echo Hello!
else echo Goodbye!
fi
always executes the echo Goodbye! command.
The TEST Command-Evaluating Boolean Expressions. Many programming languages allow statements of the form
if a_certain_boolean_condition_is_satisfied then execute_some_commands
else execute_some_other_commands
Although the TNIX shell does not perform boolean evaluations, you can use the TNIX test com-mand in conjunction with the shell's structured statements to perform the same sort of boolean evaluation.
The test command evaluates a boolean expression, then returns a true value {zero exit status} if the value of the expression is true, or a false value {non-zero exit status} if the value of expression is false. The test command returns a false value {non-zero exit status} if you do not specify an expression for test to evaluate.
The following arguments are used to construct the expression for test.
-r file true if the file exists and is readable.
-w file true if the file exists and is writeable.
-f file true if the file exists and is not a directory.
-d file true if the file exists and is a directory.
Shell Programming-8560 Series System Users Writing Shell Programs
"Structured Statements
-s
file -t number-z
string -n stringtrue if the file exists and has a size greater than zero.
true if the open file with file descriptor number (1 by default) is associated with a terminal device.
true if the length of string is zero.
true if the length of string is non-zero.
string1
=
string2 true if the values of string1 and string2 are equal.string 1 !
=
string2 true if the values of string1 and string2 are not equal.string n1-eq n2
true if string is not the null string.
true if the integers n 1 and n2 are algebraically equal. Any of the compari-sons -ne (not equal), -gt (greater than), -ge (greater than or equal to), -It (less than); or -Ie (less than or equal to) may be used in place of -eq.
These arguments may be combined with the following operators:
unary not operator (unary negation)
-a
binary and operator-0 binary or operator
( expression) used to group statements together.
You can use the if statement in conjunction with the test command to test for the existence of a file. In the following shell program, the test statement controls the output:
if test -r logfile -a -f logfile then cat logfile
elif test -f logfile
fi
then echo -logfile is not readable!-elif test -d logfile
then echo -logfile is a directory!-else echo -logfile does not
exist!-This example
• prints the logfile file, if that file exists and is readable; or
• prints a message saying that logfile exists but is not readable; or
• prints a message saying that logfile is a directory; or
• prints a message saying that logfile doesn't exist.
Writing Shell Programs
Structured Statements Shell Programming-8S60 Series System Users