I write a bash script:
#!/bin/bash
function test_echo
{
echo $0
echo $1
echo $2
echo $#
}
test_echo
I try:
find test.sh -type f -exec test_echo '{}' \;
or
find . -type f -exec `./test.sh {}` \;
but it doesn’t work.
I need a way to scan file from a folder (I use find) and for each file call a function ( foo_fonction() ) with in parameter the full path of the file.
find . -type f -exec foo_fonction '{}' \;
And how to have the parameter (full path) in the foo_fonction() ?
There are 2 ways to go about this:
1: You can put either put the function definition as part of your find files script:
(Let’s call this
find_stuff.sh)Explanation of what this does:
./find_stuff.shtest_echo()function,$(pwd)as the search directory, which will be an absolute path, the results returned byfindwill be absolute paths too – as you desired.test_echofunction2: Put the function definition elsewhere, but you still want a
find_stuff.shscript:and
test.shshould look like this:Note the last line
test_echo $@– the$@part is what you were missing before.