shell中的exec


shell中exec命令

1、find中的-exec參數

在當前目錄下(包含子目錄),查找所有txt文件並找出含有字符串"bin"的行

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

在當前目錄下(包含子目錄),刪除所有txt文件

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的內置命令命令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的內建命令exec將並不啟動新的shell,而是用要被執行命令替換當前的shell進程,並且將老進程的環境清理掉,而且exec命令后的其它命令將不再執行。

因此,如果你在一個shell里面,執行exec ls那么,當列出了當前目錄后,這個shell就自己退出了,因為這個shell進程已被替換為僅僅執行ls命令的一個進程,執行結束自然也就退出了。為了避免這個影響我們的使用,一般將exec命令放到一個shell腳本里面,用主腳本調用這個腳本,調用點處可以用bash a.sh,(a.sh就是存放該命令的腳本),這樣會為a.sh建立一個sub shell去執行,當執行到exec后,該子腳本進程就被替換成了相應的exec的命令。

source命令或者”.”,不會為腳本新建shell,而只是將腳本包含的命令在當前shell執行。

不過,要注意一個例外,當exec命令來對文件描述符操作的時候,就不會替換shell,而且操作完成后,還會繼續執行接下來的命令。

exec 3<&0:這個命令就是將操作符3也指向標准輸入。

原文

[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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM