shell script output

Cameron Simpson cs at zip.com.au
Fri Nov 24 10:46:55 UTC 2006


On 24Nov2006 14:44, Kaushal Shriyan <kaushalshriyan at gmail.com> wrote:
| Below is my script
| 
| ##########################################
| #!/bin/bash
| 
| v1=20
| v2=30
| v3=40
| 
| i=1
| 
| for i in 1 2 3
| do
| echo  $'v'$i''
| done
| 
| ##########################################
| 
| I want to print the values of v1,v2 and v3

Ok, so the command you want to issue, for $i == 2, is:

  echo $v2

So construct that string:

  shcmd="echo \$v$i"
or
  shcmd='echo $v'$i

The result of that is that the variable "$shcmd" contains the string:

  echo $v2

Then "eval" that command (which takes a string and runs it as a shell
command):

  eval "$shcmd"

So your loop would look like this:

  for i in 1 2 3
  do
    shcmd="echo \$v$i"
    eval "$shcmd"
  done

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Everything is gone;
Your life's work has been destroyed.
Squeeze trigger (yes/no)?
- Haiku Error Messages http://www.salonmagazine.com/21st/chal/1998/02/10chal2.html




More information about the users mailing list