The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
 

 Pipes/Redirections

1.0 Pipes & Redirections
       Pipes
       Redirections
           "1>" or ">" : Standard Output Redirection
           "1>>" or ">>" : Standard Output Append Redirection
           "2>" : Standard Error Output Redirect
               /dev/null : Bottomless pit
           "2>>" : STDERR Append Redirection
           "0<" or "<" : Standard Input (STDIN) Redirection
           "-" : STDIN
           "2>&1" or &>" : STDERR Redirects STDOUT
           "1>&2" : ST DOUT Redirected STDERR
           "<< Delimmiter" : End Input
           tee T-Shaped Pipeline
1.1 Instruction Return Value & Instruction Execution Flow
       "&&" : Return Value is 0 Execute
       "||" : Return Value Execute for ≠0


ENG⇒中ENG⇒中
 1.0 Pipes and Redirection

Let the Linux command-line interface possess a powerful force like nuclear fission, and the trio of pipes, redirection, and wildcards characters play crucial roles. Pipes and redirection enable individual commands with limited functionality to generate nuclear fission and catalyze powerful capabilities.

For example, the command dos2unix can be replaced by commands such as tr -d '\r' < DOS_FILE > UNIX_FILE , and its combination can be varied, which is beyond the reach of the graphical operation interface.

Pipes
The basic concept of "pipes" or "pipelines" is to treat the output of a certain Linux command as the input of another command. "Linux Pipes" simulates water pipes in the real world, and can catch water flows in different directions for reuse, so the pipes symbol of UNIX/Linux is also a water pipe "|".

Basically, the standard input (stdin) of Linux is the keyboard, and the standard output (stdout) is the screen (terminal). Take the command cat /usr/share/dict/linux.words as an example. If there is no pipe, the output is basically the screen. It is also impossible to see the output clearly. Therefore, "pipes" can be used to connect the data originally output to the screen to the page reader more or less , and finally output to the screen by more or less ; for example, cat /usr/share/dict/linux.words | less can use less read slowly, page by page.

Therefore, the basic usage of the pipes is "stdout of a certain command | stdin of a certain command", and most commands that have output to the screen can be used as input sources for another command through the pipelines.

ex:
$ ls -F /etc | grep '/$' ← list directories but not files (common)
$ ls -F /etc | grep -v '/$' | less ← list files but not directories Pipe to less
$ cat file | more ← output file to flip reader
$ echo abcd | tr [:lower:] [:upper:] ← output pipe to tr to uppercase output

How to apply the pipes is only limited by imagination. For example, if I want to query the ASCII code of a certain character or string, I can use the command od to cooperate with the pipe to achieve it.

Example: (use the pipeline to list the ASCII code of the string)
$ echo -n 'Linux' | od -An -tx1 ← List the ASCII code of the string "Linux" in hex.
4c 69 6e 75 78

The application of the pipes can also be very complicated. In theory, as long as it conforms to "the stdout of a certain command is the stdin of another command", it can be extended indefinitely. If you want to know which accounts have been created, you can use the following example.

Example:
$ cat /etc/passwd | grep '/home' | cut -d: -f1 | sort
aaa
bbb
john
austin

^ back on top ^

Redirections
There is a little introduction in the pipes that "linux standard input ( stdin) is the keyboard, and standard output (stdout) is the screen".
In more detail, the following table shows that the fd of the standard input stdin is 0. After the command or program is processed, if it can be processed normally, the message generated on the screen is the standard output stdout (fd 1). If the processing is wrong, there will be an error output to the screen, that is, stderr (fd 2).
Input   Command   Output
fd 0,stdin (standard input) programs fd 1,stdout (standard output)
fd 2,stderr (standard error)

But the "standard" of output/input can be changed, and the output/input can be redirections not necessarily the keyboard or screen. Redirections can be exported to the file.

The symbol of the pipes is only "|", which is relatively simple, but the symbol of Redrections stdin ( fd 0), stdout (fd 1) and stderr (fd 2), and append redirection, so the combination is more complicated; the following is possible The notation and usage of:

symbol action
1> or > STDOUT, because the File Descriptor of stdout  is originally 1, so "1" can be omitted
1>>or >> STDOUT append redirection
2> error output (stderr) redirection
2>> STDERR append redirection
0< 或 < STDIN redirections, because the fd stdin is originally 0, so "0" can be omitted
- standard input
2>&1 stderr redirects stdout
1>&2 或 >&2 stdout redirects stderr
<< delimmiter end of input string

Some redirection functions are a bit abstract, let's explain them one by one:

^ back on top ^

tee T-Shaped Pipeline
"1>" or ">" standard output redirection can output the original output to the screen to a file (or device file), but what if you want to output to the screen and output to a file at the same time? Just in Add a shunt pipeline to the pipeline to form a "T-shaped pipe".
The action of the "T-shaped pipeline" instruction tee is as shown in the figure below, and a shunt pipe is connected to divert the files: The usage of

Linux tee pipe
T-shaped pipeline tee is as follows:

Syntax: read_from_stdout | tee [-otpiton] filee
command name/function/command user options function
tee/
(pipe tee) T-shaped pipeline/
Any
-a output append redirection
-i ignore interrupt

example:
$ echo 'Hello World' | tee tee_file.txt ← both screen and file are output)
Hello World ←screen output
$ cat tee_file.txt ←Verify The file
Hello World

example:
$ man tee | tee -a file1 file2 file3←tee can split multiple files at one time


^ back on top ^

 
  


1.1 Instruction Return Value & Instruction Execution Flow

More than 99% of Linux is written in C language, and a feature of C language is that a value will be returned to the system when the program ends. The consensus among them is to return a value of 0 if the program executes without errors. If it is not 0, it means that the program encounters a situation during execution.
The return value has two functions, one is to know the cause of the error (non-zero return value can be checked in the table or the possible cause of the error can be known from the manpage) and the other is to judge the process of the script language .
Then how to know the return value of the command? Enter echo $? in the text interface to know the return value of the previous command.

Example: (Log in and test with a general user account)
$ cd ~root ←Enter root’s home directory
bash: cd: /root: Permission denied ←Permission denied (an error occurred)
$ echo $?←Check the command just returned value
1 ←Non-zero means the program execution error
$ cd ~ ←Enter your home directory
$ echo $? ←Check the value returned by the command just now
0 0 means the program is executed correctly

^ back on top ^

"&&" : Return Value is 0 Execute
When the command before "&&" is executed correctly (when the return value is 0), the command after "&&" will be executed. If the execution result is wrong, it will be blocked by "&&" and will not be executed

The basic usage is CMD1 && CMD2 && CMD3 && ... && CMDn , the rule is: from left to right, if the execution result of the command is correct (the return value of the command is 0

example:
# cd /root && mv /root/fileA /root/fileB && clear ←If the execution of "cd" is correct, then execute "mv"; if it is wrong, execute "clear"

"||" : Return Value Execute for ≠0
 Contrary to " && ", "||" returns a value of ≠0 for the execution result ( the execution result is not normal) before executing the following command.

example:
$ cat /etc/man.conf || cat /etc/man.config ← If "cat /etc/man.conf" is wrong, execute "cat /etc/man.config"

"&&" and "||" can be mixed and matched.
example:
$ [ -f fileA ] && [ -f fileB ] && echo "exist" || echo "Not found" ← If the result of the first command is abnormal, the commands after "&&" will not be executed but "|| "The following command


^ back on top ^