|
||||||
Not all Linux inputs have to be from the keyboard and not all Linux outputs have to be to the screen - this article looks at how to change these data streams.
Linux has two data streams that the user interacts with - even if the user doesn't realize that they are data streams; these are:
However, it is very easy to change the data streams so that:
And there is actually a third data stream - the standard error - although the user only normally sees this as part of the standard output (since all errors are displayed on the screen). Using the Standard Input and OutputBefore looking a the redirection, it's worth looking at the standard output in normal usage: $ ls -1
How to Redirect Standard Input and Output.html
process_article
writing_status
writing_status~
Here a command has been typed in via the keyboard and the result shown on the screen - the keyboard supplies the input for the standard input and the screen is the output for the standard output. Redirecting the Standard OutputThe standard output is redirected by using the > sign: $ ls -1 > files.txt
There will be no output to the screen, instead a new file will be created (in this case files.txt) and if that file is examined then it will be found to contain a list of the directory contents. If the command is repeated then it will result in an error if noclobber has been set (to prevent files from being overwritten accidentally) for instance: $ set -o noclobber
$ ls -1 > files.txt
-bash: files.txt: cannot overwrite existing file
If that's the case then the overwrite can be forced by adding an exclamation mark: $ ls -1 >! files.txt
Another option is to append the standard output to the file by using >>: $ ls -1 >> files.txt
Redirecting the Standard InputUnsurprisingly the standard input is redirected by using the < sign: $ ls -1 > files.txt
$ cat < files.txt
How to Redirect Standard Input and Output.html
process_article
writing_status
writing_status~
In many cases the < is not needed - if the command is given a file name then it will use that as then standard input, for example both of the following will use files.txt as the standard input: cat < files.txt
cat files.txt
Working with the Standard ErrorThe third data stream that Linux uses - the standard error - appears to be the same as the standard output, but it will bypass any the standard output redirection; so, for example, the following will result on an error message on the screen but not the target file: $ wc -l nofile.txt > count.txt
In this case the standard error must be redirected to the standard output and then to the file: $ wc -l nofile.txt 2>&1 1>& count.txt
The target file (count.txt) will now contain the error message. SummaryFor the normal Linux user:
However, the standard output can be redirected to a file:
And, of course, the standard input can be redirected by using the < sign. Finally the standard error needs to be handled - and will have to be passed to the standard output and then to a text file:
By combining all of these the user has complete control of each of the Linux standard data streams.
The copyright of the article Redirecting the Linux Standard Input and Output in Linux Programming is owned by Mark Alexander Bain. Permission to republish Redirecting the Linux Standard Input and Output in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||