| United States-English |
|
|
|
![]() |
Shells: User's Guide: HP 9000 Computers > Chapter 21 Basic Shell ProgrammingData Input and Output |
|
Programming inevitably requires inputting and outputting of data. The Korn Shell provides the echo command and the print command for outputting and the read command and positional parameter substitution for inputting. There are several ways of passing data into a shell script. One way is by passing arguments to the script through positional parameters; the other way is by using the read command. A third way is for the script to run some command or program that reads stderr or a named file. Positional parameters have already been described in detail in Chapter 19 “Substitution Capabilities”. Therefore, the following discussion focuses mainly on the read command. The read command provides the ability to read input during the execution of a script. The read syntax for POSIX Shell is: read [-r] name... The read syntax for Korn Shell is: read [-prsu[n]] [name?prompt] [name]... where, in each case, the command reads a line and places each whitespace-separated word into a name. The rest of the line goes into the last name. For the Korn Shell, if names are not specified, the line is read into the shell REPLY variable (see “Using the select Statement”). If ?prompt is set, the user is prompted interactively with prompt. Option definitions are:
In this script contained in the file hello_script, the first line prints a prompt and waits for input:
The read command prompts the user for a name and stores the name in the variable user_name. Running the script creates this output:
When you see the question mark, type in your name (Stefan is typed here to indicate user input), and then press Return. The read command can read and store several values at one time:
This reads the first whitespace-separated name from the input line into field1, the second into field2, and the rest into junk. Sometimes you may wish to output data or comments from a script on the screen, such as script results, and headers to describe the results. There are two output mechanisms in the shell. The first is the echo command used in Bourne, C, and POSIX Shells; and the second is the print command, unique to Korn Shell. The echo command prints its (expanded) arguments to stdout. The arguments are separated by spaces. echo [arg ]... The echo command will do parameter expansions on unquoted arguments, and on arguments in double quotes. It will not do expansion on arguments in single quotes.
You can also prompt a user from a script using the echo command and the \c linefeed escape character. The escape character suppresses the linefeed and leaves the cursor after the colon (:) and blank, waiting for input. Using this idea, type:
Certain characters can be used for formatting echoed strings. These escape sequences are listed in Table 21-1 “echo Formatting Escape Sequences”. Table 21-1 echo Formatting Escape Sequences
Korn Shell provides a unique output mechanism the other shells do not: the print command. Its syntax is: print [-Rnprsu[n]] [arg]... The print command provides a superset of the echo command for shell output. It prints the specified args, depending on the option set. The options are:
This print command:
puts the comment "# End of the day.", followed by a date, in your history file. This makes it easier to review the current day's command lines in the history file, because the end of yesterday's commands is clearly marked. (The history command lists the last sixteen command lines executed.) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||