Management of file input and output in linux

First, the definition of input and output

1. Input: Input refers to the characters entered by our keyboard, mouse and other hardware in the system

2. Output: Output refers to the characters generated by the process after the system receives the function characters we want to achieve.

                 There will be two kinds of output, the number 1 is the correct output, the number 2 is the error output, both of these outputs will be directed to the character device by the system by default.

2. How to manage output

1. Non-interactive multi-line entry

Non-interaction requires the creation of a shell script, and the interaction can be avoided by executing the script in the shell

For example: we modify the password of the root user under the operation

Create a new file, and then execute the sh file name directly

Then run it directly, change the root password to redhat

2. How to manage input and output

        >

         > Redirect correct output
         2> Redirect error output
         &> Redirect all output
         2>&1 Convert the number of error output from 2 to 1

Example: Use the student user as an experiment, find the file named passwd under /etc, save the correct output to the file.out file, and display the wrong output directly in the shell without being processed

Similarly, look for the file named passwd under /etc. The error output is saved to file.out. The correct output is not processed and displayed directly in the shell.

Use find /etc/ -name passwd &> file.all to save all output in the file.all file

Note: > redirected output will overwrite the original file, if you want to keep the original file, you need to use >> output to append

>>Add
the output to the file while keeping the original file content unchanged
>>Add the correct output
2>>Add the wrong output

&>> appends all output

For example, operate the following command, and then view the westos file

find /etc/ -name passwd >> westos
find /etc -name passwd 2>> westos

find /etc -name passwd &>> westos

All three output contents are in the westos file and are not overwritten.

3. Pipeline |

Pipes: turn the output into the input of the next program

ls /bin | wc -l count the number of lines in the file under /bin


After passing through the pipeline, the output will become the input, then the tee that cannot be saved in the file will be
copied and output to the specified location

ls /bin | tee file | wc -l Count the files under /bin and save them in the file file and display how many lines there are in the shell



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325757191&siteId=291194637