Use of the EXEC command in the shell

Source: Internet
Author: User

As a result of the recent scripting, with exec, haha, today simply record a bit to deepen the impression

EXEC Execution Program

Althoughexecand theSourceis executed directly in the parent process, butexecthis andSourceThere's a big difference,Sourceis to performShellscript, and will return to the previousShell. andexecexecution does not return to the previousShell, but directly to the previous landingShellas a program, on which the lines are copied

Note: In this case, when executing in a shell script, exec produces a state of exit, so exec is generally invoked, but when it comes to manipulating the file operator, it does not function, and after execution, it continues to remain in the current shell, such as exec 3 <&0 This means that the operator 3 is also pointed to the standard input, have not seen? Okay, now, let's introduce the syntax.


exec

< P class= "P0" style= "MARGIN-TOP:0PT;MARGIN-BOTTOM:0PT;" >exec ls

in shell ls Span style= "font-family: ' The song Body '; >, ls no return to the original after the end shell in the

exec  <>

file exec standard input

exec >file

file

exec 3 <>

file read into fd3 Span style= "font-family: ' The song Body '; >

sort <&3

fd3< Span style= "font-family: ' The song Body '; The content read in is categorized

exec 4>file

will be written to the Span style= "font-family: ' Times New Roman ';" >fd4 file

ls >&4

Ls< Span style= "font-family: ' The song Body '; > There will be no display, write directly to fd4 file in

exec 5<&4

create Span style= "font-family: ' Times New Roman ';" >fd4 fd5

exec 3<&-

close the Span style= "font-family: ' Times New Roman ';" >FD3


Let's go first. Look in the/dev/fd/directory:

There are four items by default:0 is the standard input and the default is the keyboard.

1 is the standard output, the default is the screen /dev/tty

2 is a standard error, the default is also the screen

255

When we execute EXEC 3>/root/test, and then go to see/DEV/FD, a certain number of 3, what does that mean?It also adds a device that can be realized hereLinuxthe idea of a device as a file. this timefd3is the equivalent of a pipeline, redirected tofd3the files in the file will be written inTestthe. Close this redirect can be usedexec 3>&-


In addition, this command can be used as an option for the Find command, as follows:
(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 {}


Let's use it flexibly.


Use of the EXEC command in the shell

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.