DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Automating frequent tasks

Getting input from a file or a terminal

In addition to printing information on the screen and redirecting the output from commands, you will almost certainly want to let your scripts prompt you for information, and make use of that information. The Bourne and Korn shells both provide the read command, which is the inverse of print or echo; it reads a line from a file (using the standard input as a default) and stores the successive words in the line in a series of shell variables which you specify on the command line. If you don't specify enough variables to hold all the words on the line read by print, all the remaining words will be stored in the last variable you name.

For example, suppose we use the following script to get a line of input from the terminal:

   print Hi there! Please type a line of text.\n
   read foo
   print $foo
When you run the script, it prompts for a line of text, and reads it all into the variable foo. The next line then prints the contents of foo. (Remember, to the shell, $foo means ``the contents associated with the variable named foo'', but foo on its own is simply a name; so the command print foo will output the word ``foo'', rather than the contents of the variable foo. This is a common pitfall when you start programming the shell.) For example, if the script above is called getline:
   $ ./getline
   Hi there! Please type a line of text.
   This is a test.
   This is a test.
   $
The Korn shell provides a shorthand notation for this, as follows:
   read 'foo?Hi there! Please type a line of text. '
This is equivalent to the following:
   print Hi there! Please type a line of text.
   read foo
Text up to the question mark is interpreted as the name of a variable in which the input is stored: text after the question mark is used as a prompt.

To read two words into different variables, you might use a script like the following:

   print Hi there! Type two words then press enter.\n
   read foo bar
   print The first word I read was $foo
   print and the second was $bar
If you type three words when you run this script, instead of two, the last two words will appear in the second variable. For example, if the script is called getwords:
   $ ./getwords
   Hi there! Type two words then press enter.
   hello yourself, program!
   The first word I read was hello
   and the second was yourself, program!
   $
When you use the Korn shell (but not the Bourne shell) read takes a number of options. These are as follows:

-p
Read input from a co-process. The shell disconnects from its pipe to the co-process when an error or end-of-file condition is read.

-s
Save the input line as a command in the history file (without executing the command).

-un
Read a line from file descriptor n. The default is file descriptor 0, the standard input.
For the other options and the arguments to read, refer to ksh(C).
Next topic: Reading a single character from a file or a terminal
Previous topic: More about redirecting input and output

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 03 June 2005