Another BASH Scripting question

Chris F.A. Johnson c.f.a.johnson-bJEeYj9oJeDQT0dZR+AlfA at public.gmane.org
Mon Feb 9 18:54:52 UTC 2004


On Mon, 9 Feb 2004, D. Hugh Redelmeier wrote:

> | From: Chris F.A. Johnson <c.f.a.johnson-bJEeYj9oJeDQT0dZR+AlfA at public.gmane.org>
> | On Mon, 9 Feb 2004, Madison Kelly wrote:
>
> |     Use eval:
> |
> | eval var=${server}_SNAT_IP
> | echo "$var | { .......
>
> If I understand what you are suggesting, then it does not work:
>
>     $ server=SRV2
>     $ SRV2_IB_TCP="22 80"
>     $ eval var=${server}_SNAT_IP
>     $ echo $var
>     SRV2_SNAT_IP
>
> I think that this is closer to what is needed:
>     $ eval z='$'${server}_IB_TCP
>     $ echo $z
>     22 80

    Right. I meant:

eval var=$\${server}_SNAT_IP

    That what happens when I post before my first cup of coffee.

> How can you read this ugly eval?

    It just takes a little practice.

> The operand expands into z=$SRV2_IB_TCP
> And then it is evaluated.
> The single quotes prevent the enclosed $ from being "executed" during operand
> expansion.

    Exactly. I prefer the backslash.

    It can get pretty hairy using backslashes with eval, but if I need
    more than 2 consecutive backslashes, I use a different syntax.

> My personal religion frowns on eval.  eval is a rather powerful,
> subtle, dangerous, and confusing instument.  Anything involving IFS is
> also somewhat suspect.

     Eval is indispensible, and changing IFS is frequently the best
     way to accomplish a task (though less so in bash than in an old
     Bourne shell).

> If I'd designed the Bourne Shell, the following would work:
>
>     $ echo ${${server}_IB_TCP}
>     bash: ${${server}_IB_TCP}: bad substitution

     In bash, you can use:

v=${server}_SNAT_IP
var=${!v}

-- 
	Chris F.A. Johnson
	=================================================================
	cfaj-uVmiyxGBW52XDw4h08c5KA at public.gmane.org                      http://cfaj.freeshell.org
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list