Re: shell script output

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 24Nov2006 14:44, Kaushal Shriyan <kaushalshriyan@xxxxxxxxx> 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@xxxxxxxxxx> 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

-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux