I. Input and Output 1. standard file descriptor
Each process can have a maximum of nine open file descriptors: 0 -- stdin, 1 -- stdout, 2 -- stderr.
(1) stdin
For terminals, stdin is the keyboard.
When the input redirection symbol (<) is used, the standard input file descriptor is replaced with the file referenced by the redirection.
(2) stdout
For terminals, stdout is the monitor.
When the output redirection symbol (>) is used, the standard output file descriptor is replaced with the file referenced by the redirection.
You can also use> to append data to a file.
(3) stderr
Shell processes error messages separately from normal output messages. By default, both stderr and stdout are output to the monitor.
2. Redirection Error
There are several ways to redirect data
(1) only redirect error:2>File (2 must be in front of>, between 2 and>Space is not allowedIn this case, the standard output is not affected and is still output to the display.
(2) Redirect errors and data:
Use two redirection outputs to different files at the same time,2> file1, 1> file2.
Output to the same file:&>File
2. retargeting the output in the script
You can use either of the following methods to redirect the output using scripts:
1. Temporary redirection
To redirect to a file descriptor, you must add the & sign before the number. Ex:> & 2 (> & no space between them)
After the file descriptor is redirected, the redirection output content of the script is also redirected.
2. Permanent redirection
AvailableExecCommand to notify shell to redirect specific file descriptors during Script Execution
Ex: exec 1> testout
After stdout and stderr are redirected, the text cannot be easily redirected back to the original text. You need to use the skills (see below)
3. Redirect input in the script
You can also use exec, EX:Exec 0 <File
This is a good method for reading text data and processing it using scripts.
4. Create your own redirection
Except 0, 1 and 2, other 3-8 can be used to create your own redirection.
1. Create output file descriptor
Similarly, first use exec 3> test13out, and then put a certain line ......... > & 3
2. Redirection file descriptor
You can redirect the original location of stdout to the alternative file descriptor, and then redirect the file descriptor back to stdout.
Ex: exec 3> & 1
Exec 1> File
......
Exec 1> & 3
This is a common method to temporarily redirect the script file output and restore it back to normal settings.
3. Create input file descriptor
Using the same method above, you can save stdin first, then redirect it, And then restore it.
Ex: exec 6 <& 0
Exec 0 <testfile
............
Exec 0 <& 6
4. Create a file descriptor for reading/writing
You can use the same file descriptor to read data from a file and write data to the file:
Exec 3 <> testfile
A file pointer is maintained internally to indicate the internal position. Both the read and input will occur at the position indicated by the pointer last time.
5. Disable the file descriptor.
Use&-To close the file descriptor, EX:
Exec 3> &-(you cannot use 3 again later)
If the same file is opened, the previous data will be overwritten.
5. List open file descriptors
UseLsofCommand to list open file descriptors on the entire system.
You can use $ to determine the current PID, EX: lsof-a-p $-D 0, 1, 2
6. disable command output
Redirects stderr to an empty file (/dev/null ),No output data is saved, that is, all data is lost.
You can use CAT/dev/null> testfile to quickly erase all data in testfile..
VII. Use temporary files
The/tmp directory is used to save temporary files. Files under/tmp are automatically deleted each time they are started.
AvailableMktempCommand to create a unique temporary file under/tmp
1. Create a local temporary file
Mktemp file name. xxxxxxYou can create a unique file in the local directory.
To use the mktemp command in the script, you must use a variable to save the file name.
2. Create a temporary file in/temp
UseMktemp-TYou can create a temporary file in/temp and return the complete path name.
3. Create a temporary directory
UseMktemp-dYou can create a temporary directory in the current directory.
8. record messages
You can useTeeCommand.
Ex: Tee filename sends stdin data to stdout at the same time, and the specified file filename
You can use the pipeline command configuration to redirect the output of any command: ex: Date | tee testfile
By default, Every time tee is used, the output file will be overwritten. You can use-a to add data to the file.