Linux Know-How provides a collection of introductory texts on often needed Linux skills. |
Home Bash Guide for Beginners Writing interactive scripts User Input File input and output | |||||||||||||
See also: Input/output redirection | |||||||||||||
Search the VIAS Library | Index | |||||||||||||
File input and outputUsing /dev/fdThe /dev/fd directory contains entries named 0, 1, 2, and so on. Opening the file /dev/fd/N is equivalent to duplicating file descriptor N. If your system provides /dev/stdin, /dev/stdout and /dev/stderr, you will see that these are equivalent to /dev/fd/0, /dev/fd/1 and /dev/fd/2, respectively. 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. Read and exec1. Assigning file descriptors to filesAnother 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 exec fdN> file for assigning file descriptor N to file for output, and exec fdN< file 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:
2. Read in scriptsThe following is an example that shows how you can alternate between file input and command line input:
Closing file descriptorsSince 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 exec fd<&- 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:
Here documentsFrequently, 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:
And this is how the script runs. When prompted with the "Is this ok [y/N]" string, the script answers "y" automatically:
|
|||||||||||||
Home Bash Guide for Beginners Writing interactive scripts User Input File input and output |