The content of this article requires the reader to know some of the syntax and role of the shell, the purpose of the shell, and some basic usage.
To learn shell scripting, it is important to understand the concept of pipeline and the concept of input and output of command. Only by mastering pipeline can we write better shell scripts, and this chapter introduces pipeline in detail.
The command in the shell can accept some input and then produce some output, similar to the function expression
y = f(x) in mathematics, input the parameter
x and get the result
y, the command can be regarded as a functional equation.
Standard inputs, outputs and errors
Every program touches three special files (everything is a file in linux):
stdin: abbreviation for standard input, meaning standard input, from which most programs read their input, represented by the number
stdout: abbreviation for standard output, meaning standard output, most programs write output information to this file, represented by the number
stderr: abbreviation for standard error, meaning standard error, most programs need to write error information to this file, indicated by the number
mostis used above to mean that not all programs will read and output information according to the above specification, as any program is free to choose where to read input from and write output information to.
These three special files are stored in the
dev/ (dev means device) folder.
Each time you run a program in the shell, the shell associates the keyboard with the program’s standard input and connects the standard output and standard errors with the terminal display.
From the above diagram we can see the flow of data, keyboard -> stdin file -> program -> stdout/stderr -> screen, where the flow of data is essentially a pipeline.
The pipe (
| operator can be used to redirect the output of one command to the input of another command, i.e.,
stdout of one command is used as
stdin of another command.
Re-represent the data flow of
cat test.txt | sort | uniq as a graph.
< can redirect a program’s standard input to a file, e.g.
rev < /dev/stdin, i.e. the rev command will read input from standard input, so the
rev < /dev/stdin carriage return is equivalent to the
rev direct carriage return command.
Output redirection to files
>Writes the output of a command to a file and overrides (overrides) the contents of the file.
>>appends (appends) the output of a command to the end of a file, without deleting the original contents of the file.
2>> indicate that the standard error is redirected to a file, and
2 is the file descriptor of the standard error.
Let’s try to create two identical directories in the same path and see what happens.
You can see that if you create the directory with the same name again, the shell will report an error, and the error message here is a standard error, not a standard output. We can try to use the pipe operator to see if we can redirect the standard error.
The test requires the
tr (translate characters) command, which converts characters from lowercase to uppercase, for example.
Now we redirect our error message.
As you can see the tr command does not seem to receive any standard input, this is because
mkdir js is wrong and the error message is output to the standard error, while
| only redirects the standard output.
As mentioned above
stderr all have a file descriptor of
So how do you handle standard error messages? Here are some general practices.
2>is used for standard error redirection to a file, while
stderrare special files, so here it means standard error (2) redirection to standard output (1).
2>. /errors.txt: redirects the standard error message to a file, which will overwrite the original content of the file.
2>/dev/null: redirects standard error output to
2>>. /errors.txt: Adds standard error messages to a file.
>output.txt 2>&1: redirects both standard errors and standard output to the
Let’s try to redirect the error message of the created folder to the error.txt file.
2>error.txt indicates a redirect error message, so there is no error message on the screen when
mkdir js is executed for the first time.
A great shell learning tutorial is recommended: Effective Shell.