Re: Variable resolution in shell scripts

From: Bill Thompson (bill.thompson@GOODYEAR.COM)
Date: Thu Jul 11 2002 - 10:23:22 EDT


You need to use "eval"...

   var_1="first var"
   var_2="second var"
   var_3="third var"

   read user_input

   eval "echo User selected the \${var_${user_input}}iation"

As for a book, I like "Learning the Korn Shell" published by O'Reilly &
Associates, Inc. (http://www.oreilly.com/catalog/korn2/).

Perl is a very good language to know and worth your while to learn but you
should learn shell scripting as well. There are times that a shell script
is much easier and quicker to write than a Perl script. Also, you'll
probably come across somebody else's shell script that you'll have to
debug. A good knowledge of shell scripting allows you to do thinks at the
command line you wouldn't otherwise be able to do.

Bill Thompson
Sr UNIX Systems Administrator
The Goodyear Tire & Rubber Co.

Contains Confidential and/or Proprietary Information
May Not Be Copied or Disseminated Without Express Consent of The Goodyear
Tire & Rubber Company.

AIX-L Archives: http://marc.theaimsgroup.com/?l=aix-;&r=1&w=2

----- Original Message -----
From: "Robert Bardos" <Robert.Bardos@ZUGERKB.CH>
Newsgroups: bit.listserv.aix-l
To: <aix-l@Princeton.EDU>
Sent: Thursday, July 11, 2002 11:00 AM
Subject: Variable resolution in shell scripts

>
> An easy one for most of you,
>
> given a piece of code like this:
>
> var_1="first var"
> var_2="second var"
> var_3="third var"
> read user_input
> # user enters "2"
> echo "User selected the ????iation"
> # expected result is: "User selected the second variation"
>
> Is there some kind of recursive variable resolution, ie. like
> $var_$user_input, that would resolve into the expected result and if yes,
> how would I have to code this?
>
> What book would you recommend for a beginner who wants to write some
"more
> than trivial" shell scripts? (Or should I leave it at that and learn
Pearl
> instead?)
>
> TIA
> Robert
>
>
>
> Sicherheitsmeldung: Diese Mitteilung ist ungeschuetzt und koennte
waehrend
> der Uebermittlung von Dritten eingesehen und veraendert werden. Der
Absender
> uebernimmt deshalb fuer den Inhalt keine Haftung. Sollten Sie diese
> Mitteilung irrtuemlich erhalten haben, bitten wir Sie, den Absender zu
> informieren und die Mitteilung in Ihrem System zu loeschen.
>



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:16:03 EDT