Per*_*ulf 5 shell bash find function
可能的重复:
在 find -exec 调用中执行用户定义的函数
假设我有以下 bash 代码:
!#/bin/bash
function print_echo (){
echo "This is print_echo Function" $1;
}
find ./ -iname "*" -exec print_echo {} \;
Run Code Online (Sandbox Code Playgroud)
对于每个-exec
命令,我都会收到以下错误:
find: `print_echo': No such file or directory
Run Code Online (Sandbox Code Playgroud)
注意:在此之前,我针对关键程序对其进行了测试,并使用另一种算法解决了我的程序,但这是一个问题:为什么 find 命令不接受内置 bash 命令和函数名称作为参数-exec
?
不find
接受您的函数作为命令,因为它的-exec
谓词字面上调用 C 库exec
函数来启动程序。您的函数仅适用于 bash 解释器本身。即使您在.bashrc
文件中定义了函数,它也仅对bash
.
因此,如果您确实需要两次执行 find 的-exec
一些自定义命令序列,请将其放入单独的脚本文件中或使用其他解决方法。