Re: Another bash scripting question...


Subject: Re: Another bash scripting question...
From: Guillaume Marçais (gus@nist.gov)
Date: Thu Nov 18 1999 - 12:02:38 MST


> This came up in my unix class. Part of the last scripting assignment
> was to display all the positional parameters set by bash when a script
> is called.

  One way to do this is to use the 'shift' (see bash manual) command:
while [ $# -gt 0 ]
do
        echo $1
        shift
done

  Or the 'for' statement:
for i in $*
do
        echo $i
done

>
> I wanted to do it in a loop and this little code piece is what I came up
> with to test the idea.
>
> count=1
> echo $# $*
> while [ $# -ge $count ]
> do echo ${count}
> let count=$count+1
> done
>
> If this little script is called by typing sh looptest one two three,
> the echo${count} displays the value of count (1 2 3) instead of the
> value of $1 $2 or $3, so substitution isn't occurring in the loop.

  Yes, the substitution occures in the loop. But what you want is to get 2
substitutions. One that substitute $count in 1 (or 2, or ...) and a second
one to get the value of $1 (or $2, or...) to be displayed. Under TCL, the
'eval' command is used to force the interpretor to read a second time a
piece of code.
  As an example of double substitution we can give :
[gus@dstp02:gus]$ one="finally"
[gus@dstp02:gus]$ two=one
[gus@dstp02:gus]$ echo $two
one
[gus@dstp02:gus]$ echo "echo \$$two" | sh

[gus@dstp02:gus]$ export one
[gus@dstp02:gus]$ echo "echo \$$two" | sh
finally

  'one' has to be export in order to be seen by the subshell. But this
won't work to display positionnal parameter as the subshell doesn't have
the same parameters.

  Hope it helps,
  Guillaume.



This archive was generated by hypermail 2a24 : Fri Dec 03 1999 - 19:07:33 MST