'find -exec' Linux 中的一个 shell 函数 [英] 'find -exec' a shell function in Linux
本文介绍了'find -exec' Linux 中的一个 shell 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法让 find
执行我在 shell 中定义的函数?
Is there a way to get find
to execute a function I define in the shell?
例如:
dosomething () {
echo "Doing something with $1"
}
find . -exec dosomething {} ;
结果是:
find: dosomething: No such file or directory
有没有办法让find
的-exec
看到dosomething
?
推荐答案
由于只有shell知道如何运行shell函数,所以必须运行shell才能运行函数.您还需要使用 export -f
标记要导出的函数,否则 subshell 不会继承它们:
Since only the shell knows how to run shell functions, you have to run a shell to run a function. You also need to mark your function for export with export -f
, otherwise the subshell won't inherit them:
export -f dosomething
find . -exec bash -c 'dosomething "$0"' {} ;
这篇关于'find -exec' Linux 中的一个 shell 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文