Linux exec and file descriptor tricks

  
 

For Linux, all operations on devices and files are done using file descriptors. A file descriptor is a non-negative integer that is an index value and points to a record table in which each process in the kernel opens a file. When opening an existing file or creating a new file, the kernel returns a file descriptor to the process; when it needs to read and write files, it also needs to pass the file descriptor as a parameter to the corresponding function.

Normally, when a process starts, it opens 3 files: standard input, standard output, and standard error handling. These three files correspond to file descriptors 0, 1, and 2, which are macro replacements STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO.

View and set the number of LINUX file descriptors
ulimit -nulimit -n 1024

Instance one: Assign file descriptors via exec
exec 3<>hello.txt # Read and write The method is bound to the file descriptor "3"echo "hello exec" >&3 #write"hello exec", if there is content before, it will be overwritten from the beginning of the file echo "hello world" >&3 #Write"hello world“, a new line! Exec 3>&- # Close write, prohibit write, however, it can't read it~# If it is exec 3<&-, close the read, and it can't write ~

in the above example In the file, bind the file hello.txt to descriptor 3.

Instance 2: Redirect standard output
exec 1>hello.txt # Redirect the output to the file hello.txt. From then on, the output from this script will be written to the file hello. Txtecho "hello exec"echo "hello world"

Instance 3: Redirecting standard output and restoring redirects
exec 100>&1 #Connecting file descriptor 100 to standard output# Since then To output to the terminal, we have to use a temporary descriptor to save it! Exec 1>hello.txt # redirects the output to the file hello.txt. From then on, the output from this script will be written to the file hello.txtecho "hello exec"echo "hello world"exec 1>& 100 100>&- # Connect the standard output to 100, which is the previously saved standard output # Turn off the descriptor 100, one hundred, because the standard output has been restored, it is not necessary to keep it </span>echo "oh, my god!" # From this sentence will be displayed on the terminal

Instance 4: Input Redirection
exec 100<&0exec <hello.txtread line1echo $line1read line2echo $line2exec 0< &100 100>&-read custom

Instance 5: Read and Write Files
#!/bin/bashLANG=Cecho "begin"OUTFILE="hello2.txt"INFILE="hello.txt" Function file1{<strong> </strong><strong> >$OUTFILE #zero out the file , which is equivalent to emptying the file</strong> exec 4>&1exec 1> $OUTFILEwhile read L INEdoecho "$LINE":done < $INFILEexec 1>&4exec 4>&-}file1echo "end"

with find and exec (1) in the current directory (including subdirectories), lookup All txt files and find the line containing the string "bin"
find ./-name "*.txt" -exec grep "bin" {} \\;

(2) in the current directory (including subdirectories), delete all txt files
find ./-name "*.txt" -exec rm {} \\;

Copyright © Windows knowledge All Rights Reserved