Linux tip: Using the read command
Rabu, 09 Januari 2013
0
komentar
http://www.itworld.com/development/333494/linux-tip-using-read-command
This content is excerpted from the new 3rd Ed. of 'A Practical Guide to Linux: Commands, Editors, and Shell Programming', authored by Mark Sobell, ISBN 013308504X, published by Pearson/Prentice Hall Professional, Sept. 2012, Copyright 2013 Mark G. Sobell. For more info please visit www.sobell.com or the publisher site, www.informit.com
A common use for user-created variables is storing information that a user enters in response to a prompt. Using
[Five Linux predictions for 2013 and 14 of the most useful Linux websites]
The first line of the read1 script uses
The –p (prompt) option causes
The variable in the preceding examples is quoted (along with the text string) because you, as the script writer, cannot anticipate which characters the user might enter in response to the prompt. Consider what would happen if the variable were not quoted and the user entered
The
When you do not specify a variable to receive
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:
In the following example, read2 reads a command line that calls the
If cmd does not expand into a valid command line, the shell issues an error message:
This content is excerpted from the new 3rd Ed. of 'A Practical Guide to Linux: Commands, Editors, and Shell Programming', authored by Mark Sobell, ISBN 013308504X, published by Pearson/Prentice Hall Professional, Sept. 2012, Copyright 2013 Mark G. Sobell. For more info please visit www.sobell.com or the publisher site, www.informit.com
flickr/kevin dooley
read
: Accepts User Input
A common use for user-created variables is storing information that a user enters in response to a prompt. Using read
, scripts can accept input from the user and store that input in variables. The read
builtin reads one line from standard input and assigns the words on the line to one or more variables:[Five Linux predictions for 2013 and 14 of the most useful Linux websites]
$ cat read1
echo -n "Go ahead: "
read firstline
echo "You entered: $firstline"
$ ./read1
Go ahead: This is a line.
You entered: This is a line.
echo -n "Go ahead: "
read firstline
echo "You entered: $firstline"
$ ./read1
Go ahead: This is a line.
You entered: This is a line.
The first line of the read1 script uses
echo
to prompt for a line of text. The –n option suppresses the following NEWLINE, allowing you to enter a line of text on the same line as the prompt. The second line reads the text into the variable firstline. The third line verifies the action of read
by displaying the value of firstline.The –p (prompt) option causes
read
to send to standard error the argument that follows it; read
does not terminate this prompt with a NEWLINE. This feature allows you to both prompt for and read the input from the user on one line:$ cat read1a
read -p "Go ahead: " firstline
echo "You entered: $firstline"
$ ./read1a
Go ahead: My line.
You entered: My line.
read -p "Go ahead: " firstline
echo "You entered: $firstline"
$ ./read1a
Go ahead: My line.
You entered: My line.
The variable in the preceding examples is quoted (along with the text string) because you, as the script writer, cannot anticipate which characters the user might enter in response to the prompt. Consider what would happen if the variable were not quoted and the user entered
*
in response to the prompt:$ 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
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: *
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"
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"
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
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
When you enter more words than
Table 10-4 lists some of the options supported by the read builtin.
Table 10-4
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
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.
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.
Table 10-4 read
options
Option | Function |
---|---|
–a aname | (array) Assigns each word of input to an element of array aname. |
–d delim | (delimiter) Uses delim to terminate the input instead of NEWLINE. |
–e | (Readline) If input is coming from a keyboard, uses the Readline Library to get input. |
–n num | (number of characters) Reads num characters and returns. As soon as the user types num characters, read returns; there is no need to press RETURN. |
–p prompt | (prompt) Displays prompt on standard error without a terminating NEWLINE before reading input. Displays prompt only when input comes from the keyboard. |
–s | (silent) Does not echo characters. |
–un | (file descriptor) Uses the integer n as the file descriptor that read takes its input from. Thus read –u4 arg1 arg2 is equivalent to read arg1 arg2 <&4 |
The
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.
The placement of the redirection symbol (
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
$
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
.TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Linux tip: Using the read command
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke http://androidjapane.blogspot.com/2013/01/linux-tip-using-read-command.html. Terima kasih sudah singgah membaca artikel ini.Ditulis oleh Unknown
Rating Blog 5 dari 5
0 komentar:
Posting Komentar