Standard input and output
Three standard files are usually opened automatically when executing a shell command line
The standard input file (stdin), which usually corresponds to the terminal's keyboard, the standard output file (stdout), and the standard error output file (stderr), which correspond to the terminal's screen. The process will get input data from the standard input file, output normal output data to the standard output file, and send the error message to the standard error file.
Standard input and output descriptors
- Standard input (stdin): 0
- Standard output (stdout): 1
- Standard error (STDERR): 2
Pipeline
A "pipe" is a simple sequence of commands separated by a control operator "|" or "|&".
Pipe format
[time [-p]] [!] COMMAND1 [ [| or |&] COMMAND2 ...]
Through the pipeline, the output of one command can be entered as another command.
The output of each command in the pipeline is connected to the input of the next command through a pipe. In other words, each command reads the output of the previous command. This connection is executed before any redirection specified by the command.
If you use "|&", the standard error of command 1 will be piped to the standard input of the COMMAND2; it is "2>&1 |" The shorthand. The implicit redirection of a standard error is performed after any redirection specified by the command.
# 例如:[[email protected] ~]# cat /etc/passwd|gawk -F: ‘/^r/{print $p}‘
Input and output redirection input redirection
Enter redirection using the symbol "<" to complete
Command << delimiter # Standard input read into, until meet demarcation characters stop General delimiter with EOF
Command < file 1 > file 2 #将文件1做为命令的标准输入并将标准输出重定向到文件2, the original file data will be emptied
Here-documents
This type of redirection instructs the shell to read input from the current source until it sees a line that contains only words (no trailing spaces). All rows that are read to this point are used as standard input for the command.
# here-documents 格式: <<[-]WORD HERE-DOCUMENT DELIMITER # 例如:
There are no parameter extensions, command substitutions, arithmetic extensions, or file name extensions. If you reference any character in Word, the delimiter is the result of referencing the deletion in Word, and the rows in the here document are not expanded. If the word is not referenced, all of the rows in the here document are affected by the parameter extension, command substitution, and arithmetic extension. In the latter case, the character sequence ' \newline ' is ignored, and ' \ ' must be used for the reference character ' \ ', ' $ ' and '.
Here Strings
# here strings格式: <<< WORD# 例如:
Word is extended and provides the standard input commands to it.
# 如:[[email protected] ~]# cat -n < Html.py # 查看Html.py文件的内容,一般可省略“<"符号 1 #coding:utf-8 2 from HtmlTool.tool.Html import Html 3 4 a=Html("a") 5 a.setAttribute(href="https://www.baidu.com",title="this is baidu") 6 p=Html("p") 7 p.setAttribute(,cls="a b c d e f g") 8 span=Html("span") 9 span.setAttribute(cls="a b c d e f g",data="") 10 p.children(span) 11 a.children(p) 12 a.children(span) 13
Output redirection
Output redirection is done using the symbol ">", which uses standard redirection symbols to write standard output or standard errors of a command to a file.
When a single ">" is used, the contents of the target file are erased and the input from the preceding command is written to the target file.
When using two ">", that is:>>. The content before the target file is not erased, but the output of the command is appended to the target file.
# 例如:# 命令 ls 标准输出信息将会被重定向至文件dir.txt。如果dir.txt不存在,将会被创建。# 如果dir.txt文件已存在,则其原来的内容将会被替换成ls命令的标准输出。
# 例如:# 有一个文件名为 dir.txt 的文本文件,它的内容是 1.# 现在使用两个输出重定向符号“>>” 将ls命令的标准输入追加至dir.txt# 则 dir.txt 的文件内容变成了 1.
Error redirection
By default, using the redirect symbol > only redirects the standard output. The standard error is not redirected. Unless explicitly indicated.
# 例如# ls 1> dir.txt # 将ls的标准输出重定向至文件dir.txt# ls 1>> dir.txt # 将ls的标准输出追加至文件dir.txt# ls a 2 > dir.txt # 将ls的标准错误重定向至文件dir.txt,如果ls没有找到目录a将会报错# ls a 2 >> dir.txt # 将ls的标准错误追加至文件dir.txt
REDIRECT standard errors to standard output
2>&1 standard error output redirects to standard output.
&>file standard output and standard error output are redirected to file
# 例如:# 将标准输出(文件描述符1)和标准错误(文件描述符2)定向到文件dir.txt。
Only the standard output is pointed to the file Dir.txt, because the standard error is copied standard output before the standard output is redirected to Dir.txt.
# Bash在重定向时特别处理多个文件名,如下表所述:
file |
Description |
/dev/fd/fd |
If the FD is a valid integer, then the file descriptor fd is copied. |
/dev/stdin |
File descriptor 0 is copied. |
/dev/stdout |
File descriptor 1 is copied. |
/dev/stderr |
File descriptor 2 is copied. |
/dev/tcp/host/port |
If the host is a valid host name or Internet address, and the port is an integer port number or service name, Bash attempts to open the corresponding socket's TCP connection. |
Failure to open or create a file can cause redirection to fail.
Redirects that use file descriptors greater than 9 should be used with caution because they may conflict with file descriptors that are used internally by the shell.
Linux Shell Programming (iii): Pipelines and redirects