Passing variables to functtion - $@ is correct but $1 is not


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing variables to functtion - $@ is correct but $1 is not
# 1  
Old 12-26-2017
Passing variables to functtion - $@ is correct but $1 is not

Hello I have a problem in a calling function.

Code:
#!/bin/bash
#
function SEARCH_IN_CURRENT_ITEM () {


    echo °
    echo °
    echo °
    echo "PARAM : $@"

    local B_ITEM_A
    local B_ITEM_B
   B_ITEM_A="${1}"
   echo "B_ITEM_A : $B_ITEM_A"
   B_ITEM_B="$1"
   echo "B_ITEM_B : $B_ITEM_B"

    echo "FILE : $B_ITEM_B"
    file -h  "$B_ITEM_B"
    echo
    echo

}

export -f SEARCH_IN_CURRENT_ITEM


#
#
# +--------------------+
# |    FUNCTION END    |
# +--------------------+
#
#
#

MY_HOME="$1"
echo "MY_HOME : $MY_HOME"

export A_USER A_HOME A_USER_ID

CMD="find $MY_HOME/Desktop -exec bash -c 'SEARCH_IN_CURRENT_ITEM  {} ' \; "
echo "COMMAND : $CMD"
eval "$CMD"  2> /dev/null

The output is :

Code:
°
°
°
PARAM : /home/user_install/Desktop/Computer Start Seq
B_ITEM_A : /home/user_install/Desktop/Computer
B_ITEM_B : /home/user_install/Desktop/Computer
FILE : /home/user_install/Desktop/Computer
/home/user_install/Desktop/Computer: cannot open `/home/user_install/Desktop/Computer' (No such file or directory)

The value of PARAM is correct
The value of B_ITEM_A is not correct
The value of B_ITEM_B is not correct

Any help is welcome.
# 2  
Old 12-26-2017
Code:
 B_ITEM_A="${2}"
   echo "B_ITEM_A : $B_ITEM_A"
   B_ITEM_B="$3"

Should they not be $2 and $3 (red changes)?
# 3  
Old 12-26-2017
As you have spaces within your file or directory names bash word splitting is separating the filename into separate arguments. This could become even more troublesome if the paths contain newline or quote characters.

I'm a little unsure why you need to use eval at all here, but I will assume you are trying to do some sort of tricky expansion of the find arguments and will leave it alone for now. The solution could be much simpler (and safer) without eval being involved.

In this solution I am utilizing the find -print0 option with xargs -0. This is not available on all *NIX* flavors and may cause you some portability issues.

Code:
...
CMD="find $MY_HOME/Desktop -print0"
echo "COMMAND : $CMD"
eval $CMD | xargs -0 -n 1 -I {} bash -c 'SEARCH_IN_CURRENT_ITEM "$@"' _ {}

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 12-27-2017
Coming back to your original question: I don't think that in your output the "start seq" come from the invocation shown, as that will have ONLY one single parameter: the file name(s) that find detected. So: The value of PARAM is NOT correct as shown. Please give us more detail of what you want to achieve and what data are present, if you want to pursue that approach further (and not switch to the road indicated by Chubler_XL).
# 5  
Old 01-07-2018
Quote:
Originally Posted by jim mcnamara
Code:
 B_ITEM_A="${2}"
   echo "B_ITEM_A : $B_ITEM_A"
   B_ITEM_B="$3"

Should they not be $2 and $3 (red changes)?
Hello.
First Happy new year to everybody.

No.
As I could not have a good result, I have tried 2 ways to get the only and one passed parameter.
One way with this syntax : xxxxxx="${1}"
And another way like : xxxxxx="$1"
I must confess that I don't know the difference between the 2.

---------- Post updated at 14:29 ---------- Previous update was at 14:16 ----------

Quote:
Originally Posted by RudiC
Coming back to your original question: I don't think that in your output the "start seq" come from the invocation shown, as that will have ONLY one single parameter: the file name(s) that find detected. So: The value of PARAM is NOT correct as shown. Please give us more detail of what you want to achieve and what data are present, if you want to pursue that approach further (and not switch to the road indicated by Chubler_XL).
Not at all.
The param value is correct
Code:
/home/user_install/Desktop/Computer Start Seq

. This is the name of a file "Computer Start Seq" which is kept in a folder named "Desktop" in the user home ( $HOME ) : "/home/user_install/Desktop/Computer Start Seq" .

This is what "find ....... -exec...... " pass to the called function within the '{}'

---------- Post updated at 14:47 ---------- Previous update was at 14:29 ----------

Quote:
Originally Posted by Chubler_XL
I'm a little unsure why you need to use eval at all here, but I will assume you are trying to do some sort of tricky expansion of the find arguments and will leave it alone for now. The solution could be much simpler (and safer) without eval being involved.
I use this syntax because I don't know other way to print what is the command which is going to be processed.

Quote:
Originally Posted by Chubler_XL
In this solution I am utilizing the find -print0 option with xargs -0. This is not available on all *NIX* flavors and may cause you some portability issues.
I did not really achieve what I wanted to do using xargs.

I will read the xargs man page and try to understand how your example works.

---------- Post updated at 14:56 ---------- Previous update was at 14:47 ----------

Quote:
Originally Posted by RudiC
Coming back to your original question: I don't think that in your output the "start seq" come from the invocation shown, as that will have ONLY one single parameter: the file name(s) that find detected. So: The value of PARAM is NOT correct as shown. Please give us more detail of what you want to achieve and what data are present, if you want to pursue that approach further (and not switch to the road indicated by Chubler_XL).
After many tries, it seems that the problem is solve by adding singglequote.
Bad result :
Code:
CMD="find $MY_HOME/Desktop -exec bash -c 'SEARCH_IN_CURRENT_ITEM  {} ' \; "
echo "COMMAND : $CMD"
eval "$CMD"  2> /dev/null

Good result in a real run :
Code:
CMD="find $MY_HOME/Desktop -iname "*playonlinux*"   -exec bash -c \"SEARCH_IN_CURRENT_ITEM  '{}' \" \; "
echo "COMMAND : $CMD"
eval "$CMD"  2> /dev/null

---------- Post updated at 14:57 ---------- Previous update was at 14:56 ----------

Any comments are welcome.

Last edited by Don Cragun; 01-07-2018 at 11:34 PM.. Reason: Add missing CODE tags.
# 6  
Old 01-07-2018
Your function is being called with three arguments. You can't fix that in the function. You have to fix that in the code that is calling your function!

Show us the code that invokes SEARCH_IN_CURRENT_ITEM. We would have to guess that the code that invokes your function is something like:
Code:
SEARCH_IN_CURRENT_ITEM $filename

when it needs to instead be something like:
Code:
SEARCH_IN_CURRENT_ITEM "$filename"

There is absolutely no difference between the two commands:
Code:
printf '%s\n' "$1"
printf '%s\n' "${1}"

And, there is absolutely no difference between the two commands:
Code:
printf '%s\n' "$11"
printf '%s\n' "${1}1"

But there is a huge difference between the two commands:
Code:
printf '%s\n' "$11"
printf '%s\n' "${11}"

Run the command:
Code:
set -- 1 2 3 4 5 6 7 8 9 A B C D E F

and then run the above pairs of commands to see how they work.
# 7  
Old 01-12-2018
Quote:
Originally Posted by Don Cragun
Your function is being called with three arguments.
Not at all. The function is called with one parameter (a single string) which may have or not one or more space. This single string is the name of a file which is return by the find command.

Quote:
Originally Posted by Don Cragun
Show us the code that invokes SEARCH_IN_CURRENT_ITEM.
The complete script is in thread #1

Quote:
Originally Posted by Don Cragun
There is absolutely no difference between the two commands:
Code:
printf '%s\n' "$1"
printf '%s\n' "${1}"

And, there is absolutely no difference between the two commands:
Code:
printf '%s\n' "$11"
printf '%s\n' "${1}1"

But there is a huge difference between the two commands:
Code:
printf '%s\n' "$11"
printf '%s\n' "${11}"

Run the command:
Code:
set -- 1 2 3 4 5 6 7 8 9 A B C D E F

and then run the above pairs of commands to see how they work.
I will do your tip.
Thank you for helping.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

Passing Variables

I have below data: DAY1=10202013 I am trying below but not getting the desired output: COUNT=1 DATE=DAY$COUNT echo "Date is $DATE" However output I am getting is: Date is DAY1 I wanted the output as: Date is 10202013 I tried following as well: DAY1=10202013 COUNT=1... (3 Replies)
Discussion started by: rockyr1985
3 Replies

3. Shell Programming and Scripting

Grep correct pattern with special character and variables

cat file time="north_south_east_west_08:00" location="A" start="left" status="ok" end="north" time="north_south_east_west_12:00" location="C" start="right" status="ok" end="south" time="north_south_east_west_23:00" location="G" start="left" status="ok" end="east"... (7 Replies)
Discussion started by: ctphua
7 Replies

4. Shell Programming and Scripting

Passing variables and setting them

So I'm writing a script to generate pairwise scores for how similar two strings are, and while I've been able to get it to work on a single script, I've been unable to iterate it. So suppose I have a file thus 1234567890 1234567890 1234567899 first I need to assign two lines, by their... (3 Replies)
Discussion started by: viored
3 Replies

5. Shell Programming and Scripting

Passing two variables to function

HI ,I am a new in Bash and ,I dont know how to pass a second parameter to this fuction,if the name of the passed argument is num works fine,but if I try to pass secondNum,dont recognized it thanks function check() { if(($(echo ${#num}) == 0 )) then echo No arguments passed.Try... (6 Replies)
Discussion started by: lio123
6 Replies

6. UNIX for Dummies Questions & Answers

How do I find the correct environment variables for MPI?

Hello all. I've been trying to install NWCHEM in parallel on a new cluster, and have been able to get it to work on single processors by ignoring any MPI environment variables. This is, of course, pretty worthless. So I'm starting over and trying to get thing set up right for the MPI. The key... (6 Replies)
Discussion started by: EinsteinMcfly
6 Replies

7. Shell Programming and Scripting

Passing 2 variables

Hi All, I need to pass 2 variables name 'vamskt' and 'vamsi'. Here is my question: delete from gpi.usergroup where usg_user_id in ('vamskt'); delete from gpi.userroles where uro_user_id in ('vamskt'); delete from gpi.user where usr_id in ('vamskt'); insert into gpi.user... (3 Replies)
Discussion started by: tvamsikiran
3 Replies

8. Shell Programming and Scripting

Passing string variables

HI all, Very new to shell programming and just wanted some help on how to solve the following problem. I have a small shell script which searches a given file and extracts some string parameters. I want to now be able to call this script from another shell script and somehow pass the parameters... (11 Replies)
Discussion started by: pxy2d1
11 Replies

9. Shell Programming and Scripting

passing variables

Hi, Is there any way to pass variable to a sed script.For awk we have -v option.like that do we have any way to pass variable to a sed script from a awk script or from normal script? Thanx, sounder (1 Reply)
Discussion started by: sounder123
1 Replies

10. Shell Programming and Scripting

Passing Variables to AWK

Does anybody have an explanation for the following: The following scripts runs fine on IRIX64 6.5 but has bugs on Solaris 8. #! /bin/sh echo run only on an SGI machine echo type in linenumber read j echo value read value awk -f rmspass2 level=$value $j'step1.mlf' When the script is... (5 Replies)
Discussion started by: AreaMan
5 Replies
Login or Register to Ask a Question