Exec in Shell

Source: Internet
Author: User

EXEC command in Shell

1.-exec parameter in find

In the current directory (including subdirectories), find all TXT files and find the rows containing the string "bin"

Find./-name "*. txt"-exec grep "bin "{}\;

Delete all TXT files in the current directory (including subdirectories)

Find./-name "*. txt"-exec RM {}\;

Execute Command; true if 0 status is returned. all following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. the string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone.

2. Shell built-in command Exec

#! /Bin/KSh

Export log =/tmp/test. Log

Exec >>$ log 2> & 1

Ls-l kevin.txt

Exit 0

Exec [Arg]

If Arg is present, executes ARG in place of this shell.

(ARG will replace this shell ).

Shell's built-in command exec will not start a new shell, but will replace the current shell process with the command to be executed and clean up the environment of the old process, in addition, other commands after the exec command will not be executed.

Therefore, if you execute exec ls in a shell, the shell will exit after the current directory is listed, because the shell process has been replaced by a process that only executes the LS command, the execution will naturally exit after the execution ends. To avoid this impact, we usually place the exec command in a shell script, use the main script to call this script, and use Bash. sh, (. SH is the script used to store the command. sh creates a sub shell for execution. After exec is executed, the sub-script process is replaced with the corresponding EXEC command.

The source command or "." does not create a shell for the script, but only executes the commands contained in the script in the current shell.

However, pay attention to one exception. When the exec command is used to operate the file descriptor, the shell will not be replaced, and after the operation is complete, the following command will continue to be executed.

Exec 3 <& 0: This command points Operator 3 to the standard input.

Original

[1] http://blog.chinaunix.net/uid-20652643-id-1906436.html

[2] http://zhgw01.blog.163.com/blog/static/1041481220098944425489/

[3] http://blog.csdn.net/clozxy/article/details/5818465

[4] http://www.cnblogs.com/zhaoyl/archive/2012/07/07/2580749.html

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.