Operating System - HP-UX
1753844 Members
7660 Online
108806 Solutions
New Discussion юеВ

setting an array from an existing $VAR

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

setting an array from an existing $VAR

Hi all,

I'm looking for advise on a solution I have come up with but am not sure if this is the correct way to go about it:

I may be trying to over complicate things but I am developing a wrapper script that reads from a config file which assigns an array.

here are the elements of my array:

ECM_CLUST=ECM_OAT01
set -A ECM_PORT
ECM_PORT[1]=443
ECM_PORT[2]=7943
ECM_PORT[3]=7843
ECM_PORT[4]=7080
ECM_PORT[5]=7090

The wrapper script is called with ./<scriptname>

in this case service is going to be ECM but could be SIG - there are several functions in the script that expect ${SERVICE}_.

The issue I had was assigning the array elements from ${ECM_PORTS[*]} to another array within the script function - below is the fuction

${SERVICE}_Cluster_Conf () {

SERVICE=$1
cnt=1
MAXCNT=$(eval echo \${#${SERVICE}_PORT[@]})
set -A SERVICE_PORT

while [[ ${MAXCNT} -ge ${cnt} ]]
do
eval "SERVICE_PORT[${cnt}]=\${${SERVICE}_PORT[cnt]}"
echo ${SERVICE_PORT[${cnt}]}
(( cnt += 1))
done


echo "Loading the ${SERVICE}_CLUST address/s "
dscontrol cluster add ${SERVICE}_CLUST address $CLUSIP primaryhost $PRIMIP
dscontrol port add ${SERVICE}_CLUST:`echo "${SERVICE_PORT[*]}"|tr -s " " "+"` method nat reset no
dscontrol cluster set ${SERVICE}_CLUST proportions 49 50 1 0
#
echo "Adding server machines"

dscontrol server add ${SERVICE}_CLUST:${SERVICE_PORT[1]}:$SERVER1 address $WMBIP1 mapport ${SERVICE_PORT[2]} router $AGW ret
urnaddress $RETIP
dscontrol server add ${SERVICE}_CLUST:${SERVICE_PORT[1]}:$SERVER2 address $WMBIP2 mapport ${SERVICE_PORT[3]} router $AGW ret
urnaddress $RETIP

if [[ ${MAXCNT} -gt 3 ]] ; then

cnt=4
SERVICE_MAPPORT=`expr ${SERVICE_PORT[cnt]} + 100`

while [ ${#SERVICE_PORT[@]} -ge ${cnt} ] ; do

dscontrol server add ${SERVICE}_CLUST:${SERVICE_PORT[cnt]}:$SERVER1 address $WMBIP1 router $AGW returnaddress $RE
TIP
dscontrol server add ${SERVICE}_CLUST:${SERVICE_PORT[cnt]}:$SERVER2 address $WMBIP2 mapport ${SERVICE_MAPPORT} r
outer $AGW returnaddress $RETIP

done

fi

} # ${SERVICE}_Cluster_Conf

##########################################
##########################################

the config / vars file looks like:

exndot1_sysconf () {

NETMASK1=255.255.255.0
NETMASK2=255.255.255.0
NFA=10.27.84.200
HB_ADDR=10.27.84.201
CGA=10.27.90.254
AGW=10.27.91.254
CLUSIP=10.27.90.10
RETIP=10.27.91.10
PRIMIP=10.27.84.200
WMBIP1=10.27.111.78
WMBIP2=10.27.111.79
INTER1=en1
INTER2=en2
SERVER1=wmbot1
SERVER2=wmbot2
ECM_CLUST=ECM_OAT01
set -A ECM_PORT
ECM_PORT[1]=443
ECM_PORT[2]=7943
ECM_PORT[3]=7843
ECM_PORT[4]=7080
ECM_PORT[5]=7090

SIG_PORT[1]=444
SIG_PORT[2]=7944
SIG_PORT[3]=7844

} # exndot1_sysconf

#################################
#################################

The solution have questioned myself which has taken a bit of hacking is:

SERVICE=ECM
cnt=1
MAXCNT=$(eval echo \${#${SERVICE}_PORT[@]})
set -A SERVICE_PORT

while [[ ${MAXCNT} -ge ${cnt} ]]
do
eval "SERVICE_PORT[${cnt}]=\${${SERVICE}_PORT[cnt]}"
echo ${SERVICE_PORT[${cnt}]}
(( cnt += 1))
done
443
7943
7843
7080
7090

appologies if I haven't explained well and I understand that this could be quite confusing but is there a less complicated approach I should be taking?

Thanks

Chris
hello
2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

Re: setting an array from an existing $VAR

If you want to assign arrays you can do:
X=A1
set -A A2_array -- $(eval echo "\${${X}_array[@]}" )
echo "Size A2: ${#A2_array[*]}"
echo "${A2_array[@]}"

But I can't seem to have array elements with spaces.
lawrenzo_1
Super Advisor

Re: setting an array from an existing $VAR

great stuff cheers Dennis.

regards

Chris
hello