简体   繁体   English

如何使用find -exec在.bashrc中定义的bash函数

[英]how to use a bash function defined in your .bashrc with find -exec

my .bashrc has the following function 我的.bashrc具有以下功能

function myfile {
 file $1
}
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. 谢谢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. 我完全看到你的观点,发现可以运行可执行文件,它不知道传递的参数是在脚本中定义的函数。

I will get the same error when I try to exec is on bash prompt. 当我尝试exec处于bash提示符时,我将得到相同的错误。

$ 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运行它。

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 myfile is given twice. 请注意myfile被给出两次。 The first one is the $0 parameter of the script (and in this case it can be basically anything). 第一个是脚本的$0参数(在这种情况下,它基本上可以是任何东西)。 The second one is the name of the function to run. 第二个是要运行的函数的名称。

find ./ -name *.ogv -exec bash -c 'myfile {}' \;
$ cat functions.bash
#!/bin/bash

function myecho { echo "$@"; }
function myfile { file "$@"; }
function mycat { cat "$@"; }

myname=`basename $0`
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. 当然,这些功能可能比我的例子更复杂。

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 but you 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进程的事实。

even simpiler 甚至是简单的

function myfile { echo $* ; }
export -f myfile

find . -type f -exec bash -c 'myfile "{}"'  \;

I don't think find can do this, since it's the find command itself that's executing the command, and not the shell you're currently running...so bash functions or aliases won't work there. 我不认为find可以做到这一点,因为它是执行命令的find命令本身,而不是你当前正在运行的shell ...所以bash函数或别名在那里不起作用。 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, find should do the right thing with it. 如果你把你的函数定义转换成一个名为myfile的独立bash脚本,让它成为可执行文件,然后在你的路径上安装它, find应该做正确的事情。

Child shell scripts seems to keep the parent functions so you could do a script similar to this one: 子shell脚本似乎保留了父函数,因此您可以执行与此类似的脚本:

'runit.sh' 'runit.sh'

#! /bin/bash

"$@"

then do find -name out.ogv -exec ./runit.sh myfile '{}' \\; 然后find -name out.ogv -exec ./runit.sh myfile '{}' \\; and it works! 它的工作原理! :) :)

Thanks Joao. 谢谢Joao。 This looks like very clever and elegant solution. 这看起来非常聪明和优雅的解决方案。 Little issue was that I had to source my script first to run myfile function eg I borrowed from your suggestion and made my runint.sh as follows 小问题是我必须首先使用我的脚本来运行myfile函数,例如我从你的建议中借用了我的runint.sh,如下所示

#!/bin/bash
script_name=$1
func_name=$2
func_arg=$3
source $script_name
$func_name $func_arg

Now I can run it as follows 现在我可以按如下方式运行它

$ find ./ -name *.ogv -exec ./runit.sh ~/.bashrc myfile {} \;
./out.ogv: Ogg data, Skeleton v3.0

Otherwise I was getting 否则我就到了

$ find ./ -name *.ogv -exec ./runit.sh myfile {} \;
./runit.sh: 1: myfile: not found

Anyway thanks a lot. 无论如何,非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM