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 {} \\;
In todays increasingly prominent network security issues, how to ensure the security of Ubuntu syste
Computer Store News: first Linux systems for flat panel launch For those who have tired of the N
For ease of use, we will save the Xshell session. How to transfer the session information saved on
Ext2Fsd is a good tool for reading and writing Linux partitions. This software is introduced in the
Nginx reverse proxy setup tutorial
10 shortcuts under Ubuntu Linux
Under the Linux operating system to prevent hacking skills
Detailed use of mv command under linux
Three tips for improving the efficiency of Linux operation and maintenance management
XXX is not in the sudoers file. This incident will be report
Manage your app with Laravel+Grunt+Bower
Linux FTP server build tutorial
Change time method under linux introduce
Tenth Anniversary of Red Hat Enterprise Linux
CentOS Linux uses logrotate to split management logs
Ubuntu Linux how to use the source files to install software
Mac system 1/5 malware is misleading
How to set up Win8 system positioning?
Win8 is the second operating system among our users.
About Win10 installation and upgrade method
360 browser 6.2 theater mode where to use
Update Win10 system 0x80072ee2 error solution
WinXP Workgroup Computer Unreachable Solution