Библиотека сайта rus-linux.net
The read built-in command is the counterpart of the echo and printf commands. The syntax of the read command is as follows:
NAME1 NAME2 ... NAMEN
One line is read from the standard input, or from the file descriptor supplied as an argument to the
-u option. The first word of the line is assigned to the first name,
NAME1, the second word to the second name, and so on, with leftover words and their intervening separators assigned to the last name,
NAMEN. If there are fewer words read from the input stream than there are names, the remaining names are assigned empty values.
The characters in the value of the
IFS variable are used to split the input line into words or tokens; see Section 3.4.8. The backslash character may be used to remove any special meaning for the next character read and for line continuation.
If no names are supplied, the line read is assigned to the variable
The return code of the read command is zero, unless an end-of-file character is encountered, if read times out or if an invalid file descriptor is supplied as the argument to the
The following options are supported by the Bash read built-in:
Table 8-2. Options to the read built-in
|The words are assigned to sequential indexes of the array variable
ANAME, starting at 0. All elements are removed from
ANAME before the assignment. Other
NAME arguments are ignored.
|The first character of
DELIM is used to terminate the input line, rather than newline.
|readline is used to obtain the line.
|read returns after reading
NCHARS characters rather than waiting for a complete line of input.
PROMPT, without a trailing newline, before attempting to read any input. The prompt is displayed only if input is coming from a terminal.
|If this option is given, backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation.
|Silent mode. If input is coming from a terminal, characters are not echoed.
|Cause read to time out and return failure if a complete line of input is not read within
TIMEOUT seconds. This option has no effect if read is not reading input from the terminal or from a pipe.
|Read input from file descriptor
This is a straightforward example, improving on the
leaptest.sh script from the previous chapter:
The following example shows how you can use prompts to explain what the user should enter.
Note that no output is omitted here. The script only stores information about the people Michel is interested in, but it will always say you are added to the list, unless you are already in it.
Other people can now start executing the script:
After a while, the
friends list begins to look like this:
tille 24 black anny 22 black katya 22 blonde maria 21 black --output omitted--
Of course, this situation is not ideal, since everybody can edit (but not delete) Michel's files. You can solve this problem using special access modes on the script file, see SUID and SGID in the Introduction to Linux guide.
As you know from basic shell usage, input and output of a command may be redirected before it is executed, using a special notation - the redirection operators - interpreted by the shell. Redirection may also be used to open and close files for the current shell execution environment.
Redirection can also occur in a script, so that it can receive input from a file, for instance, or send output to a file. Later, the user can review the output file, or it may be used by another script as input.
File input and output are accomplished by integer handles that track all open files for a given process. These numeric values are known as file descriptors. The best known file descriptors are stdin, stdout and stderr, with file descriptor numbers 0, 1 and 2, respectively. These numbers and respective devices are reserved. Bash can take TCP or UDP ports on networked hosts as file descriptors as well.
The output below shows how the reserved file descriptors point to actual devices:
You might want to check info MAKEDEV and info proc for more information about
/proc subdirectories and the way your system handles standard file descriptors for each running process.
When you run a script from the command line, nothing much changes because the child shell process will use the same file descriptors as the parent. When no such parent is available, for instance when you run a script using the cron facility, the standard file descriptors are pipes or other (temporary) files, unless some form of redirection is used. This is demonstrated in the example below, which shows output from a simple at script:
And one with cron:
From the previous examples, it is clear that you can provide input and output files for a script (see Section 8.2.4 for more), but some tend to forget about redirecting errors - output which might be depended upon later on. Also, if you are lucky, errors will be mailed to you and eventual causes of failure might get revealed. If you are not as lucky, errors will cause your script to fail and won't be caught or sent anywhere, so that you can't start to do any worthwhile debugging.
When redirecting errors, note that the order of precedence is significant. For example, this command, issued in
will redirect output of the ls command to the file
/var/tmp. The command
will direct both standard input and standard error to the file
spoollist. The command
directs only the standard output to the destination file, because the standard error is copied to standard output before the standard output is redirected.
For convenience, errors are often redirected to
/dev/null, if it is sure they will not be needed. Hundreds of examples can be found in the startup scripts for your system.
Bash allows for both standard output and standard error to be redirected to the file whose name is the result of the expansion of
FILE with this construct:
This is the equivalent of >
FILE 2>&1, the construct used in the previous set of examples. It is also often combined with redirection to
/dev/null, for instance when you just want a command to execute, no matter what output or errors it gives.
/dev/fd directory contains entries named
2, and so on. Opening the file
/dev/fd/N is equivalent to duplicating file descriptor N. If your system provides
/dev/stderr, you will see that these are equivalent to
The main use of the
/dev/fd files is from the shell. This mechanism allows for programs that use pathname arguments to handle standard input and standard output in the same way as other pathnames. If
/dev/fd is not available on a system, you'll have to find a way to bypass the problem. This can be done for instance using a hyphen (-) to indicate that a program should read from a pipe. An example:
The cat command first reads the file
header.txt, next its standard input which is the output of the filter command, and last the
footer.txt file. The special meaning of the hyphen as a command-line argument to refer to the standard input or standard output is a misconception that has crept into many programs. There might also be problems when specifying hyphen as the first argument, since it might be interpreted as an option to the preceding command. Using
/dev/fd allows for uniformity and prevents confusion:
In this clean example, all output is additionally piped through lp to send it to the default printer.
Another way of looking at file descriptors is thinking of them as a way to assign a numeric value to a file. Instead of using the file name, you can use the file descriptor number. The exec built-in command is used to assign a file descriptor to a file. Use
for assigning file descriptor N to
file for output, and
for assigning file descriptor N to
file for input. After a file descriptor has been assigned to a file, it can be used with the shell redirection operators, as is demonstrated in the following example:
|File descriptor 5
Using this file descriptor might cause problems, see the Advanced Bash-Scripting Guide, chapter 16. You are strongly advised not to use it.
The following is an example that shows how you can alternate between file input and command line input:
Since child processes inherit open file descriptors, it is good practice to close a file descriptor when it is no longer needed. This is done using the
syntax. In the above example, file descriptor 7, which has been assigned to standard input, is closed each time the user needs to have access to the actual standard input device, usually the keyboard.
The following is a simple example redirecting only standard error to a pipe:
Frequently, your script might call on another program or script that requires input. The here document provides a way of instructing the shell to read input from the current source until a line containing only the search string is found (no trailing blanks). All of the lines read up to that point are then used as the standard input for a command.
The result is that you don't need to call on separate files; you can use shell-special characters, and it looks nicer than a bunch of echo's:
Although we talk about a here document, it is supposed to be a construct within the same script. This is an example that installs a package automatically, eventhough you should normally confirm:
#!/bin/bash # This script installs packages automatically, using yum. if [ $# -lt 1 ]; then echo "Usage: $0 package." exit 1 fi yum install $1 << CONFIRM y CONFIRM
And this is how the script runs. When prompted with the "Is this ok [y/N]" string, the script answers "y" automatically: