Linux Standard Input/Output Tutorial

  

When you execute a shell command line in Linux, you usually open three standard files, standard input files (stdin), which usually correspond to the terminal's keyboard; standard output files (stdout) and standards. Error file (stderr), both of which correspond to the screen of the terminal. The process will get the input data from the standard input file, output the normal output data to the standard output file, and send the error information to the standard error file.

The standard input (stdin) file code is 0, the standard output (stdout) file code is 1, and the standard error (stderr) file code is 2. A major feature of Linux is that standard input/output can be redirected.

First, the output redirection output redirection refers to redirecting the standard output or standard error of the command (or executable program) to the specified file. Use the > symbol.

Example: 1, # echo "hellohello" > 1.txt There is no output on the screen at this time, the content of the 1.txt file generated in the current directory is the echo output statement.

Second, input redirection Input redirection refers to redirecting the standard input of a command (or executable program) to a specified file. That is, the input can come from a specified file without coming from the keyboard. Use the < symbol.

Example: 1, # wc -l < /etc/passwd counts the number of lines in the /etc/passwd file. The standard input for the wc command comes from the /etc/passwd file.

Three, turn to the additional use output to redirect to a file, if the file already exists, it will first clear the contents of the file, and then write the information. If you want to keep the contents of the original file, you can use the >> symbol to add an effect.

Example: 1, # echo "test1" > 2.txt# echo "test2" >> 2.txt# cat 2.txttest1test2

4. Combination use steering The combination of input and steering outputs enables more functionality.

Example: 1, # cat < 1.txt > 3.txt specifies that the standard input of the cat command comes from 1.txt, and the standard output is diverted to 3.txt. The result is the same as 3.txt and 1.txt.

2, #cat > 4.txt < 3.txt specifies that the standard output of the cat command is diverted to 4.txt, and the standard input is from 3.txt. The result is the same as 4.txt and 3.txt.

The effect is the same from the above two examples, indicating that there is no order for standard input/output redirection.

3,<< Symbol# cat > 6.sh << EOF> #! /bin/bash> echo "hello world!"> EOF Description: > The standard output is redirected to 6.sh, and the standard input is still from the default keyboard. The << symbol is used to define the terminator. Any character can be used as a terminator, generally defined as EOF. Here, when input to EOF, the input is over.

5,>/dev/null 2>&1 & What does it mean? Often seen in the shell script: > /dev /null 2 ​​> & 1 & Description: 1) /dev/null stands for empty device. 2)>/dev/null means to redirect the standard output after the previous command is executed, all of which are thrown into the empty device. 3) & means equivalent, 2>&1 means that 2's output redirection is equivalent to 1, redirecting standard error to standard output, since standard output has been redirected to /dev/null, standard error is also Will redirect to /dev/null. 4) The last & indicates that the background is running.

Copyright © Windows knowledge All Rights Reserved