LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scripting: accessing a variable stored in a variable? (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-accessing-a-variable-stored-in-a-variable-441393/)

tomolesonjr 05-03-2006 07:10 PM

Scripting: accessing a variable stored in a variable?
 
ok,
I don't know how many args are going to be input for this script.
so I want to do a while loop.

the counter <= MAX

I'm trying to get what is stored in $1, $2, etc.

filename=$"$count"

Thanks,
Tom

druuna 05-03-2006 11:41 PM

Hi,

If you use bash, $# holds the amount of parameters given. Man bash fordetails.

This is also true for ksh.

Hope this helps.

tomolesonjr 05-05-2006 06:11 PM

Thanks,

that's half.

I want a while count <= $#
do
ls $$count
done

you could write a case for $1 to $9 but ... does that make sense?

tomolesonjr 05-05-2006 06:30 PM

Quote:

Originally Posted by jlliagre
Try:
Code:

eval echo \$$y

I found this. it worked.

ioerror 05-05-2006 06:46 PM

There are a number of ways to loop over arguments

Code:

while [[ -n $1 ]]; do
      ls $1
      shift
done

or

Code:

for arg in $@; do
      ls $arg
done

or (in zsh, won't work in bash as it can't expand variables inside the {..}, apparently):

Code:

for i in {1..$#}; do
      ls $argv[$i]
done

and there are undoubtedly a few other ways too. Note, in bash you'll want to quote all the $variables in case they have spaces in them.

tomolesonjr 05-05-2006 08:47 PM

Code:

#!/bin/bash
max=$(expr $# + 1)
count=1
while [ $count -lt $max ]
do
       

        for FILE in $(eval echo \$$count)
        do

                ls -l $FILE

        done
       
count=$(expr $count + 1)

done

I got this to work. I'm going to try the other ways. cool. :)


All times are GMT -5. The time now is 08:44 AM.