When I recently read "proficient in unix shell script programming", I saw exec <$1 exec 1> $ OUTFILE. After reading it online for a long time, I finally got it done. The record is as follows.
For Linux, all operations on devices and files are performed using file descriptors.
The file descriptor is a non-negative integer, which is an index value and points to the record table for each process in the kernel to open the file.
When an existing file is opened or a new file is created, the kernel returns a file descriptor to the process. When you need to read and write files, you also need to pass the file descriptor as a parameter to the corresponding function.
Generally, when a process starts, three files are opened: standard input, standard output, and standard error handling. These three files correspond to the file descriptors 0, 1, and 2 respectively, that is, STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are replaced by macros.
View and set the number of LINUX file descriptors
Ulimit-n
Ulimit-n1024
Instance 1: allocate file descriptors through exec
Exec 3 <> hello.txt # bind to file descriptor "3" in read/write mode"
Echo "hello exec"> & 3 # write "hello exec". If there is content before, it will overwrite it from the beginning of the file.
Echo "hello world"> & 3 # write "hello world", a new row!
Exec 3> &-# disable writing and prohibit writing. However, it actually cannot be read ~
# If it is exec 3 <&-, disable reading, and it cannot write ~
In the example, the hello.txt file is bound to descriptor 3.
Example 2: Redirect standard output
Exec 1> hello.txt # redirects the output to the hello.txt file. From here, the output in this script will be written into the hello.txt file.
Echo "hello exec"
Echo "hello world"
Instance 3: Restore redirection
Exec 100> & 1 # connect file descriptor 100 to standard output
# Since it will be output to the terminal later, we have to use a temporary descriptor to save it!
Exec 1> hello.txt # redirects the output to the hello.txt file. From here, the output in this script will be written into the hello.txt file.
Echo "hello exec"
Echo "hello world"
Exec 1> & 100 100> &-# connect the standard output to 100, which is the previously saved standard output.
# Shut down the descriptor 100 for a hundred times. Because the standard output has been restored, it is unnecessary to keep it.
Echo "oh, my god! "# This sentence will be displayed on the terminal
Example 4: input redirection
Exec 100 <& 0
Exec
Read line1
Echo $ line1
Read line2
Echo $ line2
Exec 0 <& 100 100> &-
Read custom
Example 5: read and write files
#! /Bin/bash
LANG = C
Echo "begin"
OUTFILE = "hello2.txt"
INFILE = "hello.txt"
Function file1
{
> $ OUTFILE # zero out the file, which is equivalent to clearing the file
Exec 4> & 1
Exec 1> $ OUTFILE
While read LINE
Do
Echo "$ LINE"
:
Done <$ INFILE
Exec 1> & 4
Exec 4> &-
}
File1
Echo "end"
Appendix: find and exec
(1) In the current directory (including sub-Directories), find all txt files and find the rows containing the string "bin"
Find./-name "*. txt"-exec grep "bin "{}\;
(2) Delete All txt files in the current directory (including subdirectories)
Find./-name "*. txt"-exec rm {}\;