[comp.unix.questions] unix shell programming question

jonas@lkbpyr.UUCP (Jonas Heyman) (02/26/90)

Hello,

Could anyone give me some help:

On the lines with "echo" I want to have the same output.
Each time the "for loop" goes trough I want to list $str1 and $str2.
How can I do that.

Sincerely Jonas.

----------------------------------------
str1="string 1"
str2="string 2"

...
...  
...

echo $str1        #This is OK   "string 1"
echo $str2 	  #This is OK   "string 2"

for loop in 1 2
do
	aa=$str$loop     #Here I want "string 1" and "string 2"
	echo $aa

	echo $str$loop   #Here I want "string 1" and "string 2"
done
-----------------------------------------

-- 

  /jonas@lkbpyr.lkb.se/

cpcahil@virtech.uucp (Conor P. Cahill) (02/26/90)

In article <504@lkbpyr.UUCP> jonas@lkbpyr.UUCP (Jonas Heyman) writes:
>echo $str1        #This is OK   "string 1"
>echo $str2 	  #This is OK   "string 2"
>
>for loop in 1 2
>do
>	aa=$str$loop     #Here I want "string 1" and "string 2"
	^^^^^^^^^^^

>	echo $aa
>
>	echo $str$loop   #Here I want "string 1" and "string 2"
        ^^^^^^^^^^^^^^
>done

Replace the indicated lines with:

	eval aa=\$str$loop     #Here I want "string 1" and "string 2"

and

	eval echo \$str$loop   #Here I want "string 1" and "string 2"

respectively, and you will get the output you desire.  Eval just tells
the shell to read the arguments as input and execute them.  this
causes the rest of the line to be scanned twice.  On the first scan, the
\$str is converted to $str and the $loop is converted to 1 or 2. The
second scan interprets the $str1 or $str2

-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+

brad@SSD.CSD.HARRIS.COM (Brad Appleton) (02/27/90)

In article <504@lkbpyr.UUCP> jonas@lkbpyr.UUCP (Jonas Heyman) writes:
>Hello,
>
>Could anyone give me some help:
>
>On the lines with "echo" I want to have the same output.
>Each time the "for loop" goes trough I want to list $str1 and $str2.
>How can I do that.
>
>Sincerely Jonas.
>
>----------------------------------------
>str1="string 1"
>str2="string 2"
>
>...
>...  
>...
>
>echo $str1        #This is OK   "string 1"
>echo $str2 	  #This is OK   "string 2"
>
>for loop in 1 2
>do
>	aa=$str$loop     #Here I want "string 1" and "string 2"
>	echo $aa
>
>	echo $str$loop   #Here I want "string 1" and "string 2"
>done
>-----------------------------------------
>
You need to use "eval" to accomplish this. Eval is a shell builtin which
may be used to:

* Process the result of an expansion or substitution by a step that
  proceeds it during command processing

* Find the value of a parameter/variable whose value is the name of 
  another parameter/variable

* Execute a line that was read from standard input


Using "eval" in your example would result in the following
(NOTE:  I removed the leading '$' from '$str" since "str" is 
not the name of a shell variable):

--------------------------------------------------------------------
     str1="string 1"
     str2="string 2"
	.
	.
	.
     for loop in 1 2
     do
     	aa=str$loop
     	echo `eval echo '$'$aa`   ## prints "string 1" when loop=1
                                  ## and prints "string 2" when loop=2
     
     	echo `eval echo '$'str$loop`   ## same effect as above
     done
--------------------------------------------------------------------

I'm not sure of what it is you want to "echo" however. The output from 
the script segment above would be:

     string 1
     string 1
     string 2
     string 2

not:

     string 1
     string 2
     string 1
     string 2

Hope this helps!


+=-=-=-=-=-=-=-=-= "... and miles to go before I sleep." -=-=-=-=-=-=-=-=-=-+
|  Brad Appleton                       |  Harris Computer Systems Division  |
|                                      |  2101  West  Cypress  Creek  Road  |
|      brad@ssd.csd.harris.com         |  Fort  Lauderdale, FL  33309  USA  |
|     ... {uunet | novavax}!hcx1!brad  |  MailStop 161      (305) 973-5007  |
+=-=-=-=-=-=-=-=- DISCLAIMER: I said it, not my company! -=-=-=-=-=-=-=-=-=-+