如何使用 find -exec 在 .bashrc 中定义的 bash 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3056788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use a bash function defined in your .bashrc with find -exec
提问by sharrajesh
my .bashrc has the following function
我的 .bashrc 具有以下功能
function myfile {
file
}
export -f myfile
it works fine when i call it directly
当我直接调用它时它工作正常
rajesh@rajesh-desktop:~$ myfile out.ogv
out.ogv: Ogg data, Skeleton v3.0
it does not work when i try to invoke it through exec
当我尝试通过 exec 调用它时它不起作用
rajesh@rajesh-desktop:~$ find ./ -name *.ogv -exec myfile {} \;
find: `myfile': No such file or directory
is there a way to call bash script functions with exec?
有没有办法用exec调用bash脚本函数?
Any help is greatly appreciated.
任何帮助是极大的赞赏。
Update:
更新:
Thanks for the response Jim.
感谢吉姆的回应。
But that's exactly what I wanted to avoid in the first place, since I have lot of utility functions defined in my bash scripts, I wanted to use them with other useful commands like find -exec.
但这正是我首先想要避免的,因为我在 bash 脚本中定义了很多实用程序函数,我想将它们与其他有用的命令(如 find -exec)一起使用。
I totally see your point though, find can run executables, it has no idea that the argument passed is function defined in a script.
我完全明白你的意思,find 可以运行可执行文件,它不知道传递的参数是在脚本中定义的函数。
I will get the same error when I try to exec is on bash prompt.
当我尝试在 bash 提示符下执行 exec 时,我会得到同样的错误。
$ exec myfile out.ogv
I was hoping that there may be some neat trick that exec could be given some hypothetical command like "bash -myscriptname -myfunctionname".
我希望可能有一些巧妙的技巧可以给 exec 一些假设的命令,比如“bash -myscriptname -myfunctionname”。
I guess I should try to find some way to create a bash script on the fly and run it with exec.
我想我应该尝试找到某种方法来动态创建 bash 脚本并使用 exec 运行它。
采纳答案by too much php
You can get bash to run a function by putting the command into bash's StdIn:
您可以通过将命令放入 bash 的 StdIn 来让 bash 运行一个函数:
bash$ find ./ -name *.ogv -exec echo myfile {} \; | bash
The command above will work for your example butyou need to take note of the fact that all of the 'myfile...' commands are generated at once and sent to a single bash process.
上面的命令适用于您的示例,但您需要注意这样一个事实,即所有“ myfile...”命令都会同时生成并发送到单个 bash 进程。
回答by Torsten Scheck
find ./ -name *.ogv -exec bash -c 'myfile {}' \;
回答by Petr Pudlák
I managed to run it perhaps more elegantly as:
我设法以更优雅的方式运行它:
function myfile { ... }
export -f myfile
find -name out.ogv -exec bash -c '"$@"' myfile myfile '{}' \;
Notice that myfileis given twice. The first one is the $0parameter of the script (and in this case it can be basically anything). The second one is the name of the function to run.
请注意,myfile给出了两次。第一个是$0脚本的参数(在这种情况下,它基本上可以是任何东西)。第二个是要运行的函数的名称。
回答by msw
$ cat functions.bash
#!/bin/bash
function myecho { echo "$@"; }
function myfile { file "$@"; }
function mycat { cat "$@"; }
myname=`basename function myfile { echo $* ; }
export -f myfile
find . -type f -exec bash -c 'myfile "{}"' \;
`
eval ${myname} "$@"
$ ln functions.bash mycat
$ ./mycat /etc/motd
Linux tallguy 2.6.32-22-core2 ...
$ ln functions.bash myfile
$ myfile myfile
myfile: Bourne-Again shell script text executable
$ ln functions.bash myecho
$ myecho does this do what you want\?
does this do what you want?
$
where, of course, the functions can be a tad more complex than my examples.
当然,其中的函数可能比我的示例更复杂一些。
回答by Jim Lewis
I don't think findcan do this, since it's the findcommand itself that's executing
the command, and not the shell you're currently running...so bash functions or aliases
won't work there. If you take your function definition and turn it into a separate
bash script called myfile, make it executable, and install it on your path somewhere,
findshould do the right thing with it.
我不认为find可以这样做,因为是find命令本身在执行命令,而不是您当前正在运行的 shell……所以 bash 函数或别名在那里不起作用。如果您使用函数定义并将其转换为名为 的单独 bash 脚本myfile,使其可执行,并将其安装在您的路径上的某个位置,则
find应该对它做正确的事情。
回答by kdubs
even simpiler
更简单
#! /bin/bash
"$@"
回答by Jo?o Portela
Child shell scripts seems to keep the parent functions so you could do a script similar to this one:
子 shell 脚本似乎保留了父函数,因此您可以执行与此类似的脚本:
'runit.sh'
'运行.sh'
#!/bin/bash
script_name=
func_name=
func_arg=
source $script_name
$func_name $func_arg
then do find -name out.ogv -exec ./runit.sh myfile '{}' \;and it works! :)
然后做find -name out.ogv -exec ./runit.sh myfile '{}' \;,它的工作原理!:)
回答by sharrajesh
Thanks Joao. This looks like very clever and elegant solution. Little issue was that I had to source my script first to run myfile function e.g. I borrowed from your suggestion and made my runint.sh as follows
谢谢若昂。这看起来是非常聪明和优雅的解决方案。小问题是我必须首先获取我的脚本来运行 myfile 函数,例如我从你的建议中借用了我的 runint.sh 如下
$ find ./ -name *.ogv -exec ./runit.sh ~/.bashrc myfile {} \;
./out.ogv: Ogg data, Skeleton v3.0
Now I can run it as follows
现在我可以按如下方式运行它
$ find ./ -name *.ogv -exec ./runit.sh myfile {} \;
./runit.sh: 1: myfile: not found
Otherwise I was getting
否则我得到
##代码##Anyway thanks a lot.
总之非常感谢。

