1752721 Members
7001 Online
108789 Solutions
New Discussion юеВ

Assign $VAR to an array

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

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
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

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...
George Spencer_4
Frequent Advisor

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.


Matthias Zander
Advisor

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.
lawrenzo_1
Super Advisor

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