Hello, On Sat, 23 Aug 2008, Randall R Schulz wrote:
On Saturday 23 August 2008 17:24, David C. Rankin wrote:
Listmates,
I don't know if this can be done, but I was trying to get a c style for loop to dereference the counter i to the equivalent command line parameter $1, $2, etc.. Like:
for ((i=1;i<=$#;i++)); do echo "\$$i = ${$i} done
First of all, this can be done more easily as: i=1 for arg; do echo "$i: $arg"; i=$[i+1]; done or i=1; for arg; do echo "$i: $arg"; i=$((i+1)); done; the $i is only there for numbering the output.
Look into the "let" and "eval" built-ins (in the BASH manual page). Understanding BASH array variables may also help.
You should avoid eval if at all possible. Think: "eval is evil" ;) There's also ${!i}: i=1; while test $i -le $#; do echo "$i: ${!i}"; ((i+=1)); done My bash is too old for the 'for (( ... )); do ...' syntax. Nothing wrong using it, as long as you're aware that it's not portable, not even to slightly dusted bashes. And note that I use three "internal" ways to manipulate $i (without using the 'for (( .. ))' syntax). Putting the above into functions foo1 .. foo3 yields: $ for i in 1 2 3; do `eval echo "foo${i}"` a "b c" d | xargs echo; done 1: a 2: b c 3: d 1: a 2: b c 3: d 1: a 2: b c 3: d Please use 'set -x' to look at what's happening when. And compare to: $ for i in 1 2 3; do eval `echo "foo${i}"` a "b c" d | xargs echo; done And no, it doesn't seem to work with 'do foo${!i} a "b c" d'. And for other ways, you need to escape/quote the arguments to fooN, e.g.: $ for i in 1 2 3; do eval "foo${i}" 'a "b c" d' | xargs echo; done Have fun analyzing & HTH, -dnh --
The three "R"s of Microsoft support: Retry, Reboot, Reinstall You forgot one: Repeat -- Mark Atwood, Lars Balker Rasmussen -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org