var googletag = googletag || {}; googletag.cmd = googletag.cmd || []; (function() { var gads = document.createElement('script'); gads.async = true; gads.type = 'text/javascript'; var useSSL = 'https:' == document.location.protocol; gads.src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js'; var node = document.getElementsByTagName('script')[0]; node.parentNode.insertBefore(gads, node); })();

  Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

find -exec How to add additional parameter when calling a funtion

Shell Programming and Scripting


Tags
solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 10 October 2017, 6:44 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 170
Thanks: 58
Thanked 3 Times in 3 Posts
find -exec How to add additional parameter when calling a funtion

Hello

Current working script is :

Code:
#
# 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 :

Code:
#
# 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
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 October 2017, 5:17 PM EDT
Location: Aachen, Germany
Posts: 11,483
Thanks: 310
Thanked 3,561 Times in 3,276 Posts
Check your quoting... i.e. double quote the entire construct.
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 10 October 2017, 6:44 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 170
Thanks: 58
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by RudiC View Post
Check your quoting... i.e. double quote the entire construct.
I tried different things but no way.


Code:
#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" \;

    #4  
Old Unix and Linux 1 Week Ago
drl's Unix or Linux Image
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 22 October 2017, 7:19 AM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,194
Thanks: 232
Thanked 408 Times in 352 Posts
Hi.

Does this demonstration help?

Code:
#!/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:

Code:
$ ./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
The Following User Says Thank You to drl For This Useful Post:
jcdole (1 Week Ago)
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago
jcdole jcdole is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 10 October 2017, 6:44 AM EDT
Location: 64500 - france ( south-ouest )
Posts: 170
Thanks: 58
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by drl View Post
Hi.

Does this demonstration help?

Code:
.....
.....
.....
find .  -name 'f?' -exec bash -c "a_function $0 {}  $VARIABLE_1 $VARIABLE_2" \;
.....
.....
.....

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.

Code:
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.
Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago
drl's Unix or Linux Image
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 22 October 2017, 7:19 AM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,194
Thanks: 232
Thanked 408 Times in 352 Posts
Hi, jcdole

You're welcome.
Quote:
Originally Posted by jcdole
But I have read that it is a bad idea to put {} between quote inside the caller ?
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
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find: missing argument to `-exec' while redirecting using find in perl ramkumarselvam Shell Programming and Scripting 2 12-16-2011 06:48 PM
url calling and parameter passing to url in script gander_ss Shell Programming and Scripting 1 05-12-2008 09:45 PM
url calling and parameter passing to url in script gander_ss UNIX for Advanced & Expert Users 1 05-07-2008 12:34 PM
Passing a parameter while calling a script Rafael.Buria Shell Programming and Scripting 2 02-13-2008 05:27 AM
pass parameter back to calling program jthomas Shell Programming and Scripting 11 11-20-2003 07:13 AM



All times are GMT -4. The time now is 10:29 PM.