'find -exec'Linux中的shell函数 [英] 'find -exec' a shell function in Linux

查看:65
本文介绍了'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 ?

Is there a way to get find's -exec to see dosomething?

推荐答案

由于只有外壳程序知道如何运行外壳程序功能,因此您必须运行外壳程序才能运行功能.您还需要使用 export -f 标记要导出的函数,否则子shell将不会继承它们:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆