Another BASH Scripting question

Chris F.A. Johnson c.f.a.johnson-bJEeYj9oJeDQT0dZR+AlfA at public.gmane.org
Mon Feb 9 17:53:47 UTC 2004


On Mon, 9 Feb 2004, Madison Kelly wrote:

> Hi all,
>
>    Sorry for asking so many questions! I am on quite the learning curve
> at the moment! :)
>
>    I am on the last portion of my firewall script and I need something
> like an array (though judging from what I have read on array's, not
> exactly one).
>
>    Here is what I am trying to do: I have rules for multiple servers.
> Later on we will add more servers. I already will have to copy/paste the
> variables and update the data for the servers but I don't want to have
> to edit the actual script. To do this, I want to have the script read
> the value from a variable but have part of that variable name itself be
> dynamic. This is what is stumping me...
[snip]

> # Here is where things break!
>
> 		echo "${server}_SNAT_IP" | {
> 			IFS=':' read pubip inetip
> 			echo "Internal IP: "${pubip}
> 			echo "Internet IP: "${inetip}
> 		}
> 	done
> fi

    Use eval:

eval var=${server}_SNAT_IP
echo "$var | { .......

   Once you have the value, you could use set to break down the
   variable, instead of piping to read:

eval var=${server}_SNAT_IP
IFS=:
set $var
echo "Internal IP: ${1}"
echo "Internet IP: ${2}"

   Or use parameter expansion:

eval var=${server}_SNAT_IP
echo "Internal IP: ${var%:*}"
echo "Internet IP: ${var#*:}"


-- 
	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