Re: Assign $VAR to an array (148 Views)
Reply
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 1 of 5 (148 Views)
Accepted Solution

Assign $VAR to an array

Hi All,

I want to assign the following variables to an array but not sure if this would be the correct solution ....

I have a set of commands to run to configure and application and depending on how many $VAR will depend on the command to execute:

the variables in a seperate config file are:

ECM_SOAP=443
ECM_HTTP=7080
ECM_HTTP=7090
ECM_MAPPORT1=7843


in the script I have the following command:

dscontrol port add ${SERVICE}_CLUST:7090+7080+7843+443 method nat reset no

the problem is when I run the command in a loop to configure another environment:

(from the config file)

SIG_SOAP=444
SIG_HTTP=7180
SIG_HTTP=7190
SIG_MAPPORT1=7844
SIG_MAPPORT2=7944

how can I take these variables and execute the command

dscontrol port add ${SERVICE}_CLUST: method nat reset no

thanks for any help!

chris
hello
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 5 (148 Views)

Re: Assign $VAR to an array

Hi Chris:

TO assign variables to an array you can do:

#!/usr/bin/sh
echo "Enter a series of space-delimited fields to populate ARY"
read LINE
set -A ARY ${LINE}
echo "${#ARY[*]} elements exist"
echo "element_0 = ${ARY[0]}"
echo "element_1 = ${ARY[1]}"
...

Regards!

...JRF...
Please use plain text.
Frequent Advisor
George Spencer_4
Posts: 35
Registered: ‎10-14-2004
Message 3 of 5 (148 Views)

Re: Assign $VAR to an array

Would the following work?

Config file:
SOAP=443
HTTP1=7080
HTTP2=7090
MAPPORT1=7843

Script:
. fullpath/config_file

dscontrol port add ${SERVICE}_CLUST:$HTTP2+$HTTP1+$MAPPORT1+$SOAP method nat reset no

This takes the port settings from the config file, and applies them to the script. You may consider using a variable for SERVICE in the config file.


Please use plain text.
Advisor
Matthias Zander
Posts: 16
Registered: ‎04-14-1999
Message 4 of 5 (148 Views)

Re: Assign $VAR to an array

a little more flexible:

PORTS=`egrep "HTTP|MAPPORT|SOAP" configfile \
| awk -F= '{if (ports=="") {ports=$2}
else {ports=ports"+"$2}
} END{print ports}'`
dscontrol port add $SERVICE_CLUST:$PORTS method nat reset no

egrep looks for all HTTP, MAPPORT and SOAP lines in your configfile
awk uses "=" as delimiter and adds the ports in one line. So you can have as many ports as you like in your configfile.
Please use plain text.
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 5 of 5 (148 Views)

Re: Assign $VAR to an array

all examples look good and I can use variations of them all further in my script - thanks all


Chris,
hello
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation