- Ubuntu 20.04 Essentials
- Neil Smyth
- 342字
- 2021-06-11 17:39:42
9.9 Input and Output Redirection
As previously mentioned, many shell commands output information when executed. By default this output goes to a device file named stdout which is essentially the terminal window or console in which the shell is running. Conversely, the shell takes input from a device file named stdin, which by default is the keyboard.
Output from a command can be redirected from stdout to a physical file on the file system using the ‘>’ character. For example, to redirect the output from an ls command to a file named files.txt, the following command would be required:
$ ls *.txt > files.txt
Upon completion, files.txt will contain the list of files in the current directory. Similarly, the contents of a file may be fed into a command in place of stdin. For example, to redirect the contents of a file as input to a command:
$ wc –l < files.txt
The above command will display the number of lines contained in the files.txt file.
It is important to note that the ‘>’ redirection operator creates a new file, or truncates an existing file when used. In order to append to an existing file, use the ‘>>’ operator:
$ ls *.dat >> files.txt
In addition to standard output, the shell also provides standard error output using stderr. While output from a command is directed to stdout, any error messages generated by the command are directed to stderr. This means that if stdout is directed to a file, error messages will still appear in the terminal. This is generally the desired behavior, though stderr may also be redirected if desired using the ‘2>’ operator:
$ ls dkjfnvkjdnf 2> errormsg
On completion of the command, an error reporting the fact that the file named dkjfnvkjdnf could not be found will be contained in the errormsg file.
Both stderr and stdout may be redirected to the same file using the &> operator:
$ ls /etc dkjfnvkjdnf &> alloutput
On completion of execution, the alloutput file will contain both a listing of the contents of the /etc directory, and the error message associated with the attempt to list a non-existent file.
- GNU-Linux Rapid Embedded Programming
- WOW!Illustrator CS6完全自學(xué)寶典
- 西門(mén)子S7-200 SMART PLC從入門(mén)到精通
- VMware Performance and Capacity Management(Second Edition)
- CorelDRAW X4中文版平面設(shè)計(jì)50例
- Data Wrangling with Python
- STM32嵌入式微控制器快速上手
- Dreamweaver CS6精彩網(wǎng)頁(yè)制作與網(wǎng)站建設(shè)
- 氣動(dòng)系統(tǒng)裝調(diào)與PLC控制
- Extending Ansible
- 計(jì)算機(jī)組成與操作系統(tǒng)
- 從零開(kāi)始學(xué)Java Web開(kāi)發(fā)
- Mastering Exploratory Analysis with pandas
- 3ds Max造型表現(xiàn)藝術(shù)
- FANUC工業(yè)機(jī)器人配置與編程技術(shù)