先看几个例子:
在当前目录下(包含子目录),查找所有txt文件并找出含有字符串"bin"的行
find ./ -name "*.txt" -exec grep "bin" {} \;
在当前目录下(包含子目录),删除所有txt文件
find ./ -name "*.txt" -exec rm {} \;
在当前目录下,查看有无kevin.txt文件,有的话将其详细信息写入到log文件,没有的话也把错误信息写入到该log文件,需要用一个小脚本实现:
#!/bin/ksh
export LOG=/tmp/test.log
exec >> $LOG 2>&1
ls -l kevin.txt
exit 0
看了上面例子后,还是不清楚exec到底是干什么的吧!首先,这两中用法中exec是不一样的, "-exec"作为find命令的一个option, 其功能是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.
而作为shell内置命令的exec
IBM的Unix Korn Shell Scripting Reference Sheet里面
Miscellaneous built-in commands这一段里面有exec命令的介绍:
exec [arg]
If arg is present, executes arg in place of this shell.
(arg will replace this shell).
阅读(11670) | 评论(2) | 转发(0) |