----- Original Message -----
From: "David C. Rankin"
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
but obviously that just results in a bad substitution. I've been through man bash with parameter substitution, but I still can't find a way to do it. I know how to do it with a counter and "for i in "$@"", but I was trying to see if I could do it with a pure dereference. Any way?
for ((i=1;i<=$#;i++)); do echo "\$$i = ${$i} done
# load up a fake command line set aa bb cc dd "ee has spaces" ff gg n=0 for i in "$@" ;do ARGV[$n]=$i echo "\$$n = \"$i\"" ((++n)) done after this, you have an array ARGV[] that you can reference via variable in the subscript n=2 echo "ARGV[$n] = \"${ARGV[$n]}\"" To more closely follow your example, and to more clearly show what you wanted, another example * uses the c-like syntax where possible * uses the array instead of $@ to generate the display set aa bb cc dd "ee has spaces" ff gg n=1 ;for i in "$@" ;do ARGV[$n]=$i ;((++n)) ;done for ((n=1;$n<=${#ARGV[@]};++n)) ;do echo "\$$n = \"${ARGV[$n]}\"" done In ksh it's a little handier, since you can create & populate an array in one shot: * the $0 is just a placeholder so we can count from 1 later like the other examples * the < vs <= is not a typo set -A ARGV "$0" "$@" for ((n=1;$n<${#ARGV[@]};++n)) ;do echo "\$$n = \"${ARGV[$n]}\"" done In zsh it's even simpler yet, since it has a built-in argv[]: for ((n=1;$n<=${#argv[@]};++n)) ;do echo "\$$n = \"${argv[$n]}\"" done All of them produce the same: $1 = "aa" $2 = "bb" $3 = "cc" $4 = "dd" $5 = "ee has spaces" $6 = "ff" $7 = "gg" note: when I say ksh I mean ksh. Not pdksh, not the symlink to bash that bash installs by default, not the symlink to zsh that zsh installs by default. Symlinks like that are plain broken as far as I'm concerned unless the respective shells actually impliment all of ksh's features and behaviors, including performance oriented otherwise invisible fetures. (like, is the sleep built-in?) All of the examples could have used $# in place of ${#array[@]} But, Since the business end of the loop is going to use the array for the actual work, then the iterator should refer to the same actual data if possible, not some other data that just happens to / should usually be the same. Anything else is just sloppy programming / sloppy logic. -- Brian K. White brian@aljex.com http://www.myspace.com/KEYofR +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org