Previous Table of Contents Next


Interactive Input

You can ask the user to type a single line of input anywhere in a script. For the Bourne and Korn shells, use the read command followed by a variable name for interactive input.


NOTE:  You do not need to assign the variable before you use it as an argument to the read command. The following example shows the Bourne and Korn shell syntax for interactive input.


NOTE:  To suppress the newline at the end of the prompt so that users type the input on the same line as the string that is displayed, use \c at the end of the string when /usr/bin is in the path before /usr/ucb. The SunOS 4.x echo -n command works only if you have /usr/ucb in the path before /usr/bin. Otherwise, the script uses the Solaris 2.x version of the echo command. Refer to the echo(1) manual page for more information.
#!/bin/sh

echo "Enter search pattern and press Return: \c"
read filename

When the prompt appears, the script waits for input from the keyboard. When the user types input and presses Return, the input is assigned to the variable and the script continues to execute.


NOTE:  Be sure to always include a descriptive screen prompt as part of the script before you request input so that users know that the script is waiting for screen input before it continues.

For the C shell, the special variable $< waits for a value from STDIN. You can use $< anywhere you would use a variable.

For the C shell, use $< as a value to a variable, as shown below:

#!/bin/csh -f
echo "Enter search pattern and press Return: \c"
set pattern = $<

When the prompt appears, the script waits for input from the keyboard. When the user types input and presses Return, the input is assigned to the variable and the script continues to execute.

Here Documents

Sometimes a shell script requires data. Instead of having the data in a file somewhere in the system, you can include the data as part of the shell script. Including the data in the script makes it simpler to distribute and maintain the script. Such a collection of data is called a here document--the data (document) is right here in the shell script. Another advantage of a here document is that shell parameters can be substituted in the document as the shell is reading the data.

The general format of a here document is shown as follows. The format is the same for all three shells.

  lines of shell commands
  ...
command << delimiter
lines of data belonging
to the here document
delimiter
  ...
   more lines of shell commands

The here document operator << signals the beginning of the here document. The operator must be followed by a special string that delimits the input, for example <<DONE. Follow the here document operator with the list of input you want to use. The input can consist of any text, and may include variables because the shell does variable substitution on a here document. At the end of the here document, you must include the delimiter at the left margin on a single line. The shell sends everything between the two delimiters to be processed as standard input.

In the following example, the mail message specified in the here document is sent to members of the staff whose names are contained in a file named stafflist, which invites them to a party. The delimiter used is the string EOF.

#!/bin/sh

time="7:ØØ p.m."
mail -s "staff party" `cat stafflist` << EOF
Please come to our staff party being held
in the cafeteria at $time tomorrow night
EOF

Generating Output

The following sections describe how to use the echo command, quoting, and command substitution.


NOTE:  Although the echo command works in Korn shell scripts, the print command is preferred. The syntax for the print command is the same as for the echo command.

The Echo and Print Commands

Use the echo command to generate messages to display on a terminal screen. You have already seen some examples of using the echo command to display the value for a variable. You can also use the echo command to display text messages, as shown in the following portion of an interactive Bourne shell script:

#!/bin/sh
echo "Enter a pathname and press Return:"
read pathname

If you want to echo more than one message on the same line, use the \c string at the end of the line to leave the cursor at the end of the output line and to suppress the newline. The following example modifies the previous Bourne shell script so that the user types the input following the colon, instead of on the line beneath the prompt message:

#!/bin/sh
echo "Enter a pathname and press Return: \c"
read pathname

If you need to display control characters or metacharacters, you can escape them (that is, get the shell to interpret the character literally) by putting a backslash (\) in front of the character. Refer to the echo(1) manual page for information about special backslash characters (for example, \c, \n).

The Bourne and Korn shell echo command has fewer restrictions on quoting metacharacters than does the C shell echo command. The following Bourne shell script displays an error message only for the parentheses in the last line, which are not escaped with backslashes. Note that the dollar sign ($) followed by a space does not need to be escaped.

$ more echodemo
#!/bin/sh

echo Type a pathname and press Return:
echo You owe me \$35 \(Please pay\).
echo This is a $ sign (not a variable)
$ echodemo
Type a pathname and press Return:
You owe me $35 (Please pay).
This is a $ sign (Not a variable).
anything: syntax error at line 6: `(' unexpected
$


Previous Table of Contents Next