LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash script arguments (http://www.linuxquestions.org/questions/programming-9/bash-script-arguments-596213/)

R3N3G4D3 10-31-2007 11:25 PM

bash script arguments
 
I wrote a bash script that takes in an undefined number of arguments. As part of the script, I want to iterate through these variables. The problem is that I need the script to take the argument corresponding to the current iteration value. I tried "echo $$k" where $k is the iterator and received an answer equal to value stored in k, and tried "echo $"$k"" and received $ followed by variable stored in k, I also tried a bunch of other things that returned unsuccessful results, interestingly "echo $$$k" produces a large unexplainable number, a memory pointer???

So for example, say I have the following:
Code:

./script testing 123

#!/bin/bash
#script:
#desired output: testing
k=1
echo $$k #output: 1
echo $"$k" #output: $1
echo \$"$k" #output: $1

I realize I could take $@ and cut it up into separate strings using space as divider, but I really want to know if my original way of doing it is possible? Thanks.

student04 11-01-2007 12:49 AM

Cool question. Here's what I found.

Doing a "shift" in the script will remove the first argument in the argument list until none are left.

Code:

#!/bin/bash

echo "Parameters: ${@}"
echo "P1 = $1"

echo ""
echo "Shifting..."
shift
echo "Parameters: ${@}"
echo "P1 = $1"

echo ""
echo "Shifting..."
shift
echo "Parameters: ${@}"
echo "P1 = $1"

So, you could technically just shift each time you go through and use $1 (if you don't care about accessing the args anymore). Or, do a for loop on $@.

But a direct answer to your question, I'm not sure.

Read a bit in here, maybe it'll say more... ;)

http://www.tldp.org/LDP/abs/html/int...s.html#ARGLIST

-AM

bigearsbilly 11-01-2007 03:37 AM

edit: this isn't what you mean is it?

you aren't being very clear, have you a simple example?



use for
Code:

#!/bin/bash

# "$@" preserves spaces
for thing in "$@"; do
    echo $thing
done

Code:

$ 1.sh hello there old 'fellow me lad'
hello
there
old
fellow me lad
$


colucix 11-01-2007 05:04 AM

Quote:

Originally Posted by R3N3G4D3 (Post 2944079)
but I really want to know if my original way of doing it is possible? Thanks.

Yes. An example is by means of the eval command. You want to interpret $1 where 1 itself is a variable? You simply do
Code:

eval echo \$$k
the first $ sign is escaped to let literal interpretation by eval, therefore the command executed is exactly
Code:

echo $1
for k equal to 1. Another way can be using arrays, if available in your version of bash. Just assign arguments to array's elements and retrieve them with the index k, as in
Code:

#!/bin/bash
args=("$@")
k=2
echo ${args[$k]}

In this case, just take in mind that array elements are zero-based.

archtoad6 11-01-2007 09:40 AM

Yet another way ("YAW" ?), perhaps the easiest, is indirect expansion:
Code:

for X in "$@"
do
  echo ${!X}  # echos the value of the variable X
done

For more info, search the bash man page for "exclamation point".


BTW & FWIW, combining the 2 techniques will finally give me double indirection:
Code:

$ K=k1 ;  A=K ;  Z=A
$ eval echo \$${!Z}
k1


R3N3G4D3 11-01-2007 10:16 AM

Thanks for the replies guys, the "eval echo \$$k" command did exactly what I needed. I'm sure other solutions work too, but that is the easiest modification to my script.


All times are GMT -5. The time now is 03:41 AM.