Linux Command Pipeline basic teaching
The "command pipeline" (command pipeline) is a very powerful basic feature of the Linux ecosystem. It can cascade a variety of different commands and turn many basic commands into a programmable way to use, which is of great help in the operation of the system.
When a general Linux command is executed, there will be three input and output data streams, namely:
Standard input (code 0): input data required for program execution.
Standard output (code 1): The output data produced by the normal execution of the program.
Standard error output (code 2): A message used to notify the user when a program error occurs, or a message used to present the program status.
When the Linux program is running, it looks like this:
Standard input STDIN ==> Linux program ==> Standard output STDOUT
==> Standard error output STDERR
We can use the redirection feature to change the flow of these data, and add a pipe to string together different Linux programs.
Pipe
Connect the input and output of the two programs in series, using pipes. The wording of the pipeline is | , for example:
ls | nl
This line of command will lead the output of ls to nl. This command of nl will add a line number to each line of data and finally output it on the screen.
Pipeline and redirection can be mixed to output both STDOUT and STDERR to the file output:
ls | nl> output 2>&1
It is also very common to concatenate multiple programs. Use grep to filter out the file names with keywords, hand them to nl and add the line number, and finally hand them to head to output the first 5 lines of data:
ls | grep keyword | nl | head -n 5
Comments
Post a Comment