jcdole
October 9, 2017, 7:33am
1
Hello
Current working script is :
#
# my_script BEGIN
#
function a_function {
FIRST_PARAM="$1"
DO_SOMETHING "$FIRST_PARAM"
}
export -f a_function
START_HERE="/home/some_user/Documents"
find $START_HERE" -exec bash -c 'a_function "$0" ' {} \;
#
# my_script END
#
What I need is to have two more parameters in the called function :
#
# my_script_not working BEGIN
#
function a_function {
FIRST_PARAM="$1"
SECOND_PARAM="$2"
THIRD_PARAM="$3"
DO_SOMETHING "$FIRST_PARAM" "$SECOND_PARAM" "$THIRD_PARAM"
}
export -f a_function
START_HERE="/home/some_user/Documents"
VARIABLE_1="VALUE_1"
VARIABLE_2="VALUE_2"
find $START_HERE" -exec bash -c 'a_function "$0" ' {} "$VARIABLE_1" "$VARIABLE_2" \;
#
# my_script_not working END
#
I have tried to replace " by '
I have tried to remove or not "$0"
I have tried to put "$VARIABLE_1" "$VARIABLE_2" before or after {}
Any help is welcome
RudiC
October 9, 2017, 9:35am
2
Check your quoting... i.e. double quote the entire construct.
jcdole
October 9, 2017, 4:49pm
3
I tried different things but no way.
#find $START_HERE" -exec bash -c 'a_function "$0" {} $VARIABLE_1 $VARIABLE_2' \;
#find $START_HERE" -exec bash -c 'a_function {} $VARIABLE_1 $VARIABLE_2' \;
#find $START_HERE" -exec bash -c 'a_function ' "$VARIABLE_1" "$VARIABLE_2" _ {} \;
#find $START_HERE" -exec bash -c 'a_function "$0" "$VARIABLE_2" "$VARIABLE_1"' _ {} \;
#find $START_HERE" -exec bash -c 'a_function {} "$VARIABLE_2" "$VARIABLE_1"' \;
#find $START_HERE" -exec bash -c 'a_function ' "{} $VARIABLE_2 $VARIABLE_1" \;
#find $START_HERE" -exec bash -c 'a_function "$VARIABLE_2" "$VARIABLE_1"' __ {} \;
#find $START_HERE" -exec bash -c 'a_function "$0" ' "{} $VARIABLE_2 $VARIABLE_1" \;
#find $START_HERE" -exec bash -c 'a_function ' "{} $VARIABLE_2 $VARIABLE_1" \;
#find $START_HERE" -exec bash -c 'a_function ' {} "$VARIABLE_2" "$VARIABLE_1" \;
#find $START_HERE" -exec bash -c 'a_function ' "$VARIABLE_2" "$VARIABLE_1" {} \;
#find $START_HERE" -exec bash -c 'a_function "$0" ' {} "$VARIABLE_1" "$VARIABLE_2" \;
drl
October 9, 2017, 5:23pm
4
Hi.
Does this demonstration help?
#!/usr/bin/env bash
# @(#) s1 Demonstrate calling function from find -exec.
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C
function a_function {
FIRST_PARAM="$1"
SECOND_PARAM="$2"
THIRD_PARAM="$3"
fourth="$4"
echo DO_SOMETHING "$FIRST_PARAM" "$SECOND_PARAM" "$THIRD_PARAM" "$fourth"
}
pl " Current files f?:"
echo f?
export -f a_function
START_HERE="/home/some_user/Documents"
VARIABLE_1="VALUE_1"
VARIABLE_2="VALUE_2"
pl " Results:"
# find $START_HERE" -exec bash -c 'a_function "$0" ' {} "$VARIABLE_1" "$VARIABLE_2" \;
find . -name 'f?' -exec bash -c "a_function $0 {} $VARIABLE_1 $VARIABLE_2" \;
producing:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.9 (jessie)
bash GNU bash 4.3.30
-----
Current files f?:
f1 f2 f3
-----
Results:
DO_SOMETHING ./s1 ./f1 VALUE_1 VALUE_2
DO_SOMETHING ./s1 ./f2 VALUE_1 VALUE_2
DO_SOMETHING ./s1 ./f3 VALUE_1 VALUE_2
I used local directory with only f* files for brevity.
Best wishes ... cheers, drl
1 Like
jcdole
October 10, 2017, 6:38am
5
Ok that the trick.
But I have read that it is a bad idea to put {} between quote inside the caller ?
The following work also but the data return by {} is found at $0 in the function.
find . -name 'f?' -exec bash -c "a_function $VARIABLE_1 $VARIABLE_2" {} \;
As far you know that, it is not a problem.
Thank you very much for helping.
drl
October 10, 2017, 8:50am
6
Hi, jcdole
You're welcome.
Where did you see this? The reading I have done suggests that {} may need to be placed within quotes to protect them from evaluation by the shell that is running the find
command.
Best wishes ... cheers, drl