gpt4 book ai didi

linux - 'find -exec' Linux 中的一个 shell 函数

转载 作者:IT老高 更新时间:2023-10-28 12:21:17 26 4
gpt4 key购买 nike

有没有办法让 find 执行我在 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 标记要导出的函数,否则 subshel​​l 不会继承它们:

export -f dosomething
find . -exec bash -c 'dosomething "$0"' {} \;

关于linux - 'find -exec' Linux 中的一个 shell 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4321456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com