Previous Table of Contents Next


Module 112
read (sh, ksh), readonly (ksh) / set $<(csh)

DESCRIPTION

The internal read command reads one line from the standard input. Since it is an internal command it assigns the input to variables. The readonly command is used to set variables to readonly mode. If a variable is readonly, you cannot change it after the readonly command has assigned it a value.

COMMAND FORMAT

Following is the general format of the read and readonly commands.

     read [ var1 var2... ]
     readonly [ var1 var2... ]

The readonly command assigns values to the listed variables and then sets them to readonly mode so the values cannot be changed later in the shell.


Csh Shell
set var=$<

Arguments

The following arguments may be passed to the read command.

var1 The first variable in the list of variables. The first word on the input line is assigned to the first variable. Each input word is assigned to the corresponding variable on the read command line. All remaining input words are assigned to the last variable.
If no variables are given the input is discarded.

Csh Shell
var The variable var is assigned the contents of the line read from standard input.

FURTHER DISCUSSION

The read command assigns each word of the input line to separate variables if they exist. The first word is assigned to the first variable, the second word to the second variable, and so on until all words are assigned. The last variable is assigned all remaining words on the input line. For example, if you typed

       cj> read ONE TWO THREE

and then typed

       cj> This line has too many words for the available variables.

the assignments of the three variables would be as follows:

       cj> echo $ONE
       This
       cj> echo $TWO
       line
       cj> echo $THREE
       has too many words for the available variables.

RELATED COMMANDS

Refer to the line command described in Module 74.

RELATED FILES

The read command reads from the standard input.

RETURN CODES

A return code of 1 is returned if an EOF(Ctrl-D) is encountered. If the read completes, then a zero value is returned.

APPLICATIONS

The read command is used to read input from the standard input, usually your keyboard. It is primarily used for interactive shell scripts. If you need to ask questions or input data, you can use the read command to read the data from the keyboard. For example, the following lines of code might be used in a shell script for a user to enter his/her name.

      cj> echo "Please enter your name(Last First MI): \c"
      read LAST FIRST MIDINIT

This assigns the first word of the input to the LAST variable, the second word of input to the FIRST variable, and the remainder of the input to MIDINIT.

TYPICAL OPERATION

In this activity you use the read command to read lines from a file that has been redirected as standard input. Begin at the shell prompt.

1.  Type read LINE < /etc/passwd and press Return. This reads the first line of the /etc/passwd file into the variable LINE.

C Shell
The csh has no internal way to read a line from a file. Here are some possible alternatives.
      set LINE=`head -1 /etc/passwd`
      set LINE=`line <  /etc/passwd`
      set LINE=`sed  1q /etc/passwd`

2.  To display the LINE variable type echo $LINE and press Return.
3.  To read from your keyboard type read ANS and press Return.

C Shell
3. To read from your keyboard type set ANS=$< and press Return.

4.  Type yes and press Return.
5.  Type echo $ANS and press Return to display the value of the ANS variable.
6.  Turn to Module 42 to continue the learning sequence.


Previous Table of Contents Next