在Linux上找到-exec一个shell函数? [英] find -exec a shell function in Linux?

查看:71
本文介绍了在Linux上找到-exec一个shell函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让find执行我在shell中定义的功能?例如:

Is there a way to get find to execute a function I define in the shell? For example:

dosomething () {
  echo "doing something with $1"
}
find . -exec dosomething {} \;

结果是:

find: dosomething: No such file or directory

有没有办法让find-exec看到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"' {} \;

这篇关于在Linux上找到-exec一个shell函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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