Linux tip: Using the read command

If you write shell scripts, this is one cool builtin Linux command you should know.

Page 3 of 3

$ cat read1_no_quote read -p "Go ahead: " firstline echo You entered: $firstline $ ./read1_no_quote Go ahead: * You entered: read1 read1_no_quote script.1 $ ls read1     read1_no_quote     script.1

The

ls
command lists the same words as the script, demonstrating that the shell expands the asterisk into a list of files in the working directory. When the variable $firstline is surrounded by double quotation marks, the shell does not expand the asterisk. Thus the read1 script behaves correctly:

$ ./read1 Go ahead: * You entered: *

REPLY

When you do not specify a variable to receive

read
's input,
bash
puts the input into the variable named REPLY. The following read1b script performs the same task as read1:

$ cat read1b read -p "Go ahead: " echo "You entered: $REPLY"

The read2 script prompts for a command line, reads the user's response, and assigns it to the variable cmd. The script then attempts to execute the command line that results from the expansion of the cmd variable:

$ cat read2 read -p "Enter a command: " cmd $cmd echo "Thanks"

In the following example, read2 reads a command line that calls the

echo
builtin. The shell executes the command and then displays Thanks. Next read2 reads a command line that executes the
who
utility:

$ ./read2 Enter a command: echo Please display this message. Please display this message. Thanks $ ./read2 Enter a command: who max     pts/4      2013-06-17 07:50 (:0.0) sam     pts/12     2013-06-17 11:54 (guava) Thanks

If cmd does not expand into a valid command line, the shell issues an error message:

$ ./read2 Enter a command: xxx ./read2: line 2: xxx: command not found Thanks

The read3 script reads values into three variables. The

read
builtin assigns one word (a sequence of nonblank characters) to each variable:

$ cat read3 read -p "Enter something: " word1 word2 word3 echo "Word 1 is: $word1" echo "Word 2 is: $word2" echo "Word 3 is: $word3" $ ./read3 Enter something: this is something Word 1 is: this Word 2 is: is Word 3 is: something

When you enter more words than

read
has variables,
read
assigns one word to each variable, assigning all leftover words to the last variable. Both read1 and read2 assigned the first word and all leftover words to the one variable the scripts each had to work with. In the following example,
read
assigns five words to three variables: It assigns the first word to the first variable, the second word to the second variable, and the third through fifth words to the third variable.

$ ./read3 Enter something: this is something else, really. Word 1 is: this Word 2 is: is Word 3 is: something else, really.

Table 10-4 lists some of the options supported by the read builtin.

The

read
builtin returns an exit status of 0 if it successfully reads any data. It has a nonzero exit status when it reaches the EOF (end of file).

The following example runs a while loop from the command line. It takes its input from the names file and terminates after reading the last line from names.

$ cat names Alice Jones Robert Smith Alice Paulson John Q. Public $ while read first rest > do > echo $rest, $first > done < names Jones, Alice Smith, Robert Paulson, Alice Q. Public, John $

The placement of the redirection symbol (

<
) for the while structure is critical. It is important that you place the redirection symbol at the done statement and not at the call to
read
.

| 1 2 3 Page 3
ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon