Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

my .bashrc has the following function

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

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?

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.

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 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".

I guess I should try to find some way to create a bash script on the fly and run it with exec.

like image 666
sharrajesh Avatar asked Jun 16 '10 20:06

sharrajesh


Video Answer


1 Answers

find ./ -name *.ogv -exec bash -c 'myfile {}' \;
like image 185
Torsten Scheck Avatar answered Sep 26 '22 03:09

Torsten Scheck