Previous Table of Contents Next


Quoting

All three shells use the same syntax for quoting, as shown in Table 16-11. Quoting a string incorrectly can cause many unexpected results.

Table 16-11 Quoting

Character Term Description
\ Backslash Nullifies the special meaning of any shell metacharacter, including another backslash.
`` Backquotes Substitutes the output as if it were typed in place of the command. Refer to the shell manual pages for more information.
'' Single quotes Nullifies the special meaning of all characters except bang (!), the backslash (\), and the single quote itself ('). Single quotes are more restrictive than double quotes and do not permit variable or backquote expansion.
"" Double quotes Nullifies the special meaning of all special characters except bang (!), backquote (``), and dollar sign ($). Permits variable and backquote expansion.


NOTE:  The bang (!) is not used as a metacharacter in the Bourne and Korn shells. If it is not quoted properly, it is likely to cause problems mostly in the C shell.

The following examples demonstrate the result of different forms of quotation:

$ name=Fred
$ echo "My name is $name"
My name is Fred
$ echo 'My name is $name'
My name is $name
$ echo "Today is `date`"
Today is Fri Jul  9 12:12:35 PDT 1993
$ echo 'Today is `date`'
Today is `date`
$ echo 'Use metacharacters * ? < > | & and $ often'
Use metacharacters * ? < > | & and $ often
$ echo "It's hard to turn off"!
It's hard to turn off!

Command Substitution

You can substitute the output of the command in place of the command itself. This process is called command substitution. Use backquotes ( ``) to surround the desired command. You can use command substitution to return the output of the command, or to use it as the value for a variable.


NOTE:  In the Korn shell, you can use the $(command) syntax instead of backquotes.

In the first example following, the output of the date command is used as part of a larger output string, and the output of the who command is filtered through wc, which counts the number of lines in the output of who to determine how many users are logged in. The second example pipes the output of the who command to the cut command to display only the list of usernames and uses the uname -n command to display the name of the system.

$ echo Today is `date` and there are `who | wc -l` users logged in
Today is Fri Jul 9 13:12:41 PDT 1993 and there are 5 users logged in

$ echo `who | cut -f1 -d" "` logged onto `uname -n`
winsor newton fred george anna logged onto seachild
$

Testing for Conditions

When writing scripts, you frequently want to test for conditions. The simplest test is to determine whether a condition is true or false. If the expression is true, execute any subsequent commands (shown indented in Table 16-12); if not, continue with the script. Table 16-12 shows the syntax for conditional tests.

Table 16-12 Conditional Test Syntax

Bourne and Korn Shells C Shell
if command if (cond) then
then commands
commands else if (cond) then
elif command commands
commands else
elsefi commands
commands endif
fi

if-then-else-elif

For the Bourne and Korn shells, use the if-then-else-elif-fi syntax to test for conditions. You can follow the if statement with the test command and its argument(s) to test for conditions. As an alternative to typing the test command, you can enclose the test condition in square brackets [ ]. You must put a space after the first bracket and before the last one for the characters to be interpreted correctly--for example:

if [ -r filename ]
then

The results of test -r filename and [ -rfilename] are identical. Refer to the section "Test and C Shell Built-in Test" in Chapter 17 for test command options.

The Bourne shell fragment shown next uses the simplest form of the if statement to test whether a user has entered at least one command-line argument following the name of the script:

#!/bin/sh
#
# Test for at least one command-line argument
#
if test $# -lt 1
then
    echo Usage: $Ø name requires one command-line argument
    exit 1
fi

If you want the script to perform additional actions if the first conditional test fails, use the else clause, as shown in the following Bourne shell fragment:

#!/bin/sh

if test $# -lt 1
then
    echo Usage: $Ø name requires one command-line argument
    exit 1
else
    echo "Thank you for entering arguments"
    echo "You entered $# arguments"
fi

You can use the elif conditional (a combination of else and if) to test for additional conditions within the if statement . The elif clause is performed if only if the previous if or else fails. Each elif clause lists another command to be tested. The following Bourne shell example has one elif and one else clause:

#!/bin/sh
#
# Time of day greetings
#
hour=`date +%H`

if [ $hour -H 12 ]
then
    echo "Good Morning!"
elif [ $hour -H 17 ]
then
    echo "Good Afternoon!"
else
    echo "Good Night!"
fi


Previous Table of Contents Next