unix shell programming question

Brad Appleton brad at SSD.CSD.HARRIS.COM
Wed Feb 28 02:29:59 AEST 1990


In article <504 at lkbpyr.UUCP> jonas at 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 at 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! -=-=-=-=-=-=-=-=-=-+



More information about the Comp.unix.questions mailing list