Re: Variable resolution in shell scripts

From: Dave Simmonds (dave.simmonds@FORTEL.COM)
Date: Thu Jul 11 2002 - 11:05:09 EDT


First, the $var= ... lines should not have a $ at the start

You can do this bit:

case $user_input in
 1)
   var=$var_1;;
 2)
   var=$var_2;;
 3)
   var=$var_3;;
 *)
   var="invalid var";;
esac

in one line:

eval var='$var_'$user_input

except it doesn't check for invalid input.

Dave Simmonds

-----Original Message-----
From: Steve Thornhill [mailto:steve_thornhill@UK.IBM.COM]
Sent: 11 July 2002 15:35
To: aix-l@Princeton.EDU
Subject: Re: Variable resolution in shell scripts

var_1="first var"
var_2="second var"
var_3="third var"
read user_input
# user enters "2"
case $user_input in
 1)
   $var=$var_1;;
 2)
   $var=$var_2;;
 3)
   $var=$var_3;;
 *)
   $var="invalid var";;
esac
echo "User selected the ${var}iation"
# expected result is: "User selected the second variation"

Steve Thornhill - IBM CLS
Int - 255104
Ext - 023 92 565104
Mob - 07778 289770
Email/Sametime - steve_thornhill@uk.ibm.com

Robert Bardos <Robert.Bardos@ZUGERKB.CH>

Robert Bardos <Robert.Bardos@ZUGERKB.CH>
Sent by: IBM AIX Discussion List <aix-l@Princeton.EDU>
11/07/2002 16:00
Please respond to IBM AIX Discussion List

To: aix-l@Princeton.EDU
cc:
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.

#### InterScan_Disclaimer.txt has been removed from this note on July 11
2002 by Steve Thornhill



image001.gif
image002.gif

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