Multi-Instruction execution
Symbol
|
Format |
Role |
; |
Comd1;comd2 |
Execute sequentially |
&& |
COMD1 && COMD2 |
COMD1 properly executed, COMMD2 executed, Comm1 execution fails, COMMD2 does not execute |
| | |
comd1 | | Comd2 |
COMM1 execution failed, execution commd1 COMM1 execution succeeds and does not execute COMMD2 |
The difference between a pipe (|) and a redirect (>>)
Pipeline Command The operator is: "|", it can only handle the correct output from the previous instruction, that is, the standard output information, for the Stdandard error information does not directly handle the ability. Then, pass to the next command as standard input.
Command1 correct output, as input of command2 and then comand2 output as, comand3 input, Comand3 output will be displayed directly on the screen.
After pipeline: The correct output of the Comand1,comand2 is not displayed on the screen
Attention:
1, Pipeline command only handle the correct output of the previous command, do not handle error output
2, the pipe command to the right, you must be able to receive the standard input stream command.
Instance:
[[email protected] shell]$ cat test.sh | grep -n ' echo ' 5: echo "very good!"; 7: echo "good!"; 9: echo "pass!"; 11: echo "no pass!"; #读出test. sh file contents, forwarded to grep as input via pipeline [[email protected] shell]$ cat test.sh test1.sh | grep -n ' echo ' cat: test1.sh: does not have that file or directory 5: echo "very good!"; 7: echo "good!"; 9: echo "pass!"; 11: echo "no pass!"; #cat test1.sh does not exist, the error output prints to the screen, and the correct output is sent to Grep [[email protected] shell]$ cat through the pipeline test.sh test1.sh 2>/dev/null | grep -n ' echo ' 5: echo "very good!"; 7: echo "good!"; 9: echo "pass!"; 11: echo "no pass!"; #将test1 .sh did not find the error output redirect output to the/dev/null file, the correct output is sent through the pipeline to Grep [[email protected] shell] $ cat test.sh | lscatfile httprequest.txt secure test testfdread.sh testpipe.sh testsh.sh Testwhile2.shenvcron.txt python sh testcase.sh testfor2.sh testselect.sh test.txt text.txtenv.txt release sms testcronenv.sh testfor.sh test.sh testwhile1.sh# reads the test.sh content and sends it to the LS command through a pipeline, because ls does not support standard input, So the data is discarded
:sed,awk , Cut,head,top,less,more,wc,join,sort,split, and so on, are text processing commands.
The difference is:
1, the left command should have standard output | The command on the right should accept the standard input
The command on the left should have standard output > the right can only be a file
The command on the left should require standard input < The right can only be a file
2, pipeline trigger two sub-Process Execution "|" On both sides of the program, and redirection is performed within a process
[[email protected] shell]$ cat test.sh| grep -n ' echo ' 5: echo "very good!"; 7: echo "good!"; 9: echo "pass!"; 11: echo "no pass!"; #"|" Both sides of the pipeline must be shell commands [[email protected] shell]$ grep -n ' echo ' <test.sh 5: echo "very good!"; 7: echo "good!"; 9: echo "pass!"; 11: echo "no pass!"; # "REDIRECT" symbol, the right can only be file (normal file, file descriptor, file device) [[email protected] shell]$ mail -s ' test ' [email protected] <test.sh[[email protected] shell]$ cat test.sh|mail -s ' Test ' [email protected] #以上2个也相同 to send test.sh content to the specified mailbox. [[email protected] shell]$ (sed -n ' 1, $p ' |grep -n ' echo ') <test.sh 5: echo "very good!"; 7: echo "good!"; 9: echo "pass!"; 11: echo "no pass!"; #这个脚本比较有意思了. Since the front is the pipeline, the test.sh content needs to be redirected to &NBSP;SED&NBSP, and then the SED output is piped through to grep. You need to enclose the preceding "()" operator. The commands within the parentheses can be seen as a command-like form. If no parentheses test.sh is the input of grep . #上面一个等同于这个 [[email protected] shell]$ sed -n ' 1, $p ' <test.sh | grep -n ' echo ' 5: echo "very good!"; 7: echo "good!"; 9: echo "pass!"; 11: echo "no pass!"; #重定向运算符, before the shell command parsing, first check (a command, before executing must check its input, output, that is, the 0,1,2 device is ready), so priority will be highest [[email protected] shell]$ sed -n ' 1,10p ' <test.sh | grep -n ' echo ' <testsh.sh10:echo $total;18:echo $total;21: echo "OK"; #哈哈, This grepAlso accepts the pipeline input, also has the testsh.sh input, that is not 2 each receives. Just now, the "<" operator takes precedence, and grep binds the testsh.sh input before the pipeline sends the data, so the SED command output is discarded. Be careful here. Use #输出重定向 [[email protected] shell]$ cat test.sh>test.txt[[email protected] shell] cat test.sh|tee test.txt &>/dev/null# the results into a file via a pipeline, Also need to use the command tee, it will be piped up standard input to file test.txt , and then copy the standard input to standard output (stdout), so redirect to/dev/null does not show output # ">" Output redirection, Often at the far right of the command, the output of the left command is received, and the result is redirected to the specified file. You can also use the middle of the command. [[email protected] shell]$ ls test.sh test1.sh testsh.sh 2>err.txt | grep ' test ' test.shtestsh.sh# directory below: Test,testsh file, test1.sh not present, so ls command error output input to Err.txt The correct output is also sent to the grep command via a pipeline. [[Email protected] shell]$ ls test.sh test1.sh testsh.sh &>err.txt | grep ' Test ' #这次打印结果是空,& represents correct and error output both input to Err.txt, passing the data through the pipeline continues to empty, so there is nothing to show the # Same ">" Output redirection, precedence is resolved first, and when a command has this character, it is bound to the standard output of the left command. When this is ready, wait for the command to execute the output data, and it begins to receive
The difference between a Linux pipe directive (pipe) and a shell redirect