From owner-freebsd-questions@FreeBSD.ORG Wed Jul 1 19:16:35 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 6E69D1065670 for ; Wed, 1 Jul 2009 19:16:35 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from poseidon.ceid.upatras.gr (poseidon.ceid.upatras.gr [150.140.141.169]) by mx1.freebsd.org (Postfix) with ESMTP id DB5C08FC12 for ; Wed, 1 Jul 2009 19:16:34 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from mail.ceid.upatras.gr (unknown [10.1.0.143]) by poseidon.ceid.upatras.gr (Postfix) with ESMTP id 74089EB4E39; Wed, 1 Jul 2009 22:16:30 +0300 (EEST) Received: from localhost (europa.ceid.upatras.gr [127.0.0.1]) by mail.ceid.upatras.gr (Postfix) with ESMTP id 694A1450E9; Wed, 1 Jul 2009 22:16:30 +0300 (EEST) X-Virus-Scanned: amavisd-new at ceid.upatras.gr Received: from mail.ceid.upatras.gr ([127.0.0.1]) by localhost (europa.ceid.upatras.gr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id kiVEcd3x0ZAU; Wed, 1 Jul 2009 22:16:30 +0300 (EEST) Received: from kobe.laptop (adsl307-134.kln.forthnet.gr [188.4.31.134]) by mail.ceid.upatras.gr (Postfix) with ESMTP id A30C6450D0; Wed, 1 Jul 2009 22:16:29 +0300 (EEST) Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.3/8.14.3) with ESMTP id n61JGP9g000437; Wed, 1 Jul 2009 22:16:25 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.3/8.14.3/Submit) id n61JGPEg000436; Wed, 1 Jul 2009 22:16:25 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) From: Giorgos Keramidas To: Alexandre Vieira References: <755cb9fc0907011040o28b82cdbjd5760b139f797050@mail.gmail.com> Date: Wed, 01 Jul 2009 22:16:25 +0300 In-Reply-To: <755cb9fc0907011040o28b82cdbjd5760b139f797050@mail.gmail.com> (Alexandre Vieira's message of "Wed, 1 Jul 2009 18:40:00 +0100") Message-ID: <87tz1wqkmu.fsf@kobe.laptop> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: freebsd-questions@freebsd.org Subject: Re: scripting tip needed X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Jul 2009 19:16:35 -0000 On Wed, 1 Jul 2009 18:40:00 +0100, Alexandre Vieira wrote: > Hi folks, > > I'm having a little problem. > > For exambe in ksh: > > $ z=0 > $ y=1 > $ x=aaaa > $ eval `echo MACHINE_DISK$z[$y]`=$x > $ echo ${MACHINE_DISK0[1]} > aaaa > > My problem is getting back the value of the variable using variables to > refer to the variable name (confusing). I.e $MACHINE_DISK$z[$y] . > > Using ${} works great when you only have a simple array but adding a > variable in the middle of the variable name seems to ruin it. > > $ echo ${MACHINE_DISK0[$y]} > aaaa > $ echo ${MACHINE_DISK$z[$y]} > ksh: ${MACHINE_DISK$z[$y]}: bad substitution My own short piece of (slightly off-topic) advice is: ``If you find yourself using arrays in bash, ksh or another shell, now is a *very* good time to consider a more advanced scripting language, like Perl or Python.'' Trying to coerce shell quoting to do a sensible thing with array syntax is error-prone, annoying and --very often-- a waste of your time: Using an interactive language like Python you can actually *test* the code as you are writing it. This is a major win most of the time.