当前位置:网站首页>Standard input, standard output, standard error redirection and pipeline
Standard input, standard output, standard error redirection and pipeline
2022-04-22 06:00:00 【Take a break and come on】
1. The standard input 、 standard output 、 The standard error
Text stream exists in Linux In every process . When Linux When starting a process , Will automatically open three ports : mark Quasi input (Standard Input)、 standard output (Standard Output) And standard mistakes (Standard Error). Processes usually input and output through these three ports .
With bash Process, for example , A running bash It's a process . By default ,bash The standard input is connected to the keyboard , Standard output and standard error are connected to the screen . For a program , Although it always opens these three streams , But he will use it as needed , It doesn't have to be used .
Such as :
$ ls # After getting the name of the folder under the current path , Combine these documents into a piece of text , Print on the terminal with standard output
$ rm none-exits-file # Delete a nonexistent file , The error message will be output through the standard error port :rm: cannot remove 'none-exits-file': No such file or directory
2. Reorientation
When bash When running a command , This bash A child process will be created to run the command . By default , Because the standard output of the subprocess is the same as bash identical , So the output appears in bash window . If you want text to flow to a file instead of being displayed on the screen , Then we can use Redirect (redirect) The mechanism of .
- standard output
$ ls > output.log # Use > take ls Import the output text stream into a file
$ ls >> output.log # Use >> take ls Import the output text stream into a file
Be careful : > and >> stay output.log When there is no , The two are the same , They are all new output.log And import the text stream into . If output.log When there is no , Use >> Text will be added at the end . Use > A new file will be created , Overlay existing files .
- The standard error
Single > and >> By default, symbols will only redirect standard output . Use 1 For standard input ,2 Represents standard error . Or use &> Redirect both standard input and standard output .
$ rm none-exit-file 2> error.log # Output the error to error.log In file
$ ls 1> output.log 2> error.log # Import the standard output stream into a file output.log, Output standard error to error.log
$ ls &> output_error.log # Point standard output and standard error to the same file .
Be careful :1> No spaces in between
- The standard input
Use < To redirect standard input
$ grep abc < content.txt # Will file content.txt Input into the standard input stream , Will include “abc” Output a line of to standard output
$ grep abc < content.txt &> output.txt # While redefining the standard input , Redefine standard output and standard error
3. The Conduit
The Conduit (pipe) It also changes the direction of the text flow . The destination of the pipeline is another process . Borrow pipe , We can turn the output of one process into the input of another process . In this way, we can pipe multiple commands together . So that they can process the text stream continuously like a pipeline . stay bash in , We use | Said the pipe .
$ ps | grep test # ps The output text stream Through pipes Import to grep Command to filter Will include test Import rows into standard output
$ grep Tom < input.txt | sort # Find all contained text from the file “Tom” The line of , And sort according to the alphabet
$ ls | grep txt | wc -l # Output... In the current directory , contain txt Total number of files
版权声明
本文为[Take a break and come on]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220541213884.html
边栏推荐
- flask 【GET/POST】数据处理
- Three lines of recursive output code of stack C
- 为CPropertySheet窗口添加最小化按钮
- Blue Bridge Cup 31 day sprint Day10
- LeetCode 2044. Count the number of subsets that can get the maximum value by bit -- depth traversal
- 08 - program input and output
- Blue bridge sprint topic - BFS
- Deep understanding of callback functions
- Golang learning and school recruitment experience
- Optimization theory: transportation problem (I) finding the minimum freight [northwest corner method, minimum element method, Vogel method]
猜你喜欢

日常学习记录——读取自定义数据集

ubuntu20.0.4下在终端安装数据库

Torch uses stepping on the pit diary and matrix to speed up the operation

PyQT5总结

Golang calculates the number of days rounded to time

STM32学习笔记2——设置GPIO寄存器实现流水灯

Torch recurrent neural network nn. RNN () and torch nn. RNNCell()

STM32学习笔记4——HC_SR04超声波测距模块的调试记录

Blue Bridge Cup 31 day sprint Day8

软件测试相关基础知识
随机推荐
09 - process control - judgment statement
B / S architecture
写一篇关于ddt数据驱动的自动化测试
golang 把句中的每个单词的首字母变成大写(笔试题)
What compiler is used for beginners of C language (there is a surprise at the end of the article)
C/S架构
QT信号与槽的特点和用法
pykmip测试
Golang calculates the number of days rounded to time
马斯克更新微信推送(狗狗币)
Apple CMS custom docking WeChat official account
12 - 容器-字符串
golang学习和校招经历
Meilisearch usage record
2021 408 考研大纲更改项
Chessboard coverage problem (divide and conquer)
标准输入、标准输出、标准错误 重定向及管道
苹果cms设置本地播放器 ckplayer(版本:ckplayerx)
06-数据类型
AIX上安装gcc并使用