Linux exec and file description Fu Jian with tips (RPM)

Source: Internet
Author: User

Recently, when I was watching "Unix shell scripting," I saw exec<$1 exec 1> $OUTFILE and I was blindfolded. Online to see the majority of days, finally fix, record as follows.
For Linux, all operations on devices and files are performed using file descriptors.
The file descriptor is a non-negative integer that is an index value and points to the record table for each process in the kernel that opens the file.
When an existing file is opened or a new file is created, the kernel returns a file descriptor to the process, and when the file needs to be read and written, it needs to pass the file descriptor as a parameter to the corresponding function.
Typically, when a process starts, 3 files are opened: standard input, standard output, and standard error handling. These 3 files correspond to file descriptors of 0, 1, and 2, which are macros that replace Stdin_fileno, Stdout_fileno, and Stderr_fileno.
View and set the number of Linux file descriptors
Ulimit-n
ulimit-n1024

Instance one: Assigning file descriptors via exec
EXEC 3<>hello.txt # is bound to the file descriptor "3" in read-write mode
echo "Hello exec" >&3 # writes "Hello exec", if previous content, this will be overwritten from the beginning of the file
echo "Hello World" >&3 # write "Hello World", New line!
EXEC 3>&-# closed write, forbidden to write, however, in fact it can not read ~
# if it's exec 3<&-, read off, and it can't write ~
In the example above, the file Hello.txt is bound to descriptor 3.
Example two: Redirecting standard output
EXEC 1>hello.txt # Redirects the output to the file Hello.txt, and thereafter the output from the script is written to the file Hello.txt
echo "Hello exec"
echo "Hello World"

Example three: resuming redirects

Exec 100>&1 # connecting the file descriptor 100 to the standard output
# We have to use a temporary descriptor to save it because it will be output to the terminal later!
Exec 1>hello.txt # redirects the output to file Hello.txt, and thereafter the output from this script will be written to the file Hello.txt
echo "Hello exec"
echo "Hello World"
Exec 1>&100 100>&-# connects the standard output to 100, which is the standard output previously saved
# will descriptor 100 off, solve everything, because the standard output has been restored, it is not necessary to keep it
echo "Oh, My god! "# starting from this sentence will be displayed on the terminal

Instance four: Enter redirect
exec 100<&0
exec Read line1
Echo $line 1 Read Line2
Echo $line 2
exec 0<&100 100>&-
Read Custom
Instance five: 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 emptying it
EXEC 4>&1
EXEC 1> $OUTFILE
While Read line
Do
echo "$LINE"
:
Done < $INFILE
EXEC 1>&4
EXEC 4>&-
}
File1
echo "End"


Find and Exec Magical
(1) in the current directory (including subdirectories), find 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 {} \;

http://blog.csdn.net/chaofanwei/article/details/19110739

Linux exec and file description Fu Jian with tips (RPM)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.