I have a sample file which looks like below.
cat sample
'c2.url0' : 'k8s-abc1.amazonaws.com : 1000'
'c2.url1' : 'k8s-abc2.amazonaws.com : 1001'
'c2.url2' : 'k8s-xyz1.amazonaws.com : 1003'
From this I want to get urls and assign it to a variable with same name as key.(i.e)if I do echo $c2.url0 then output should be "k8s-abc1.amazonaws.com : 1000".Similarly for other keys.
echo $c2.url0 should print --> "k8s-abc1.amazonaws.com : 1000"
echo $c2.url1 should print --> "k8s-abc2.amazonaws.com : 1001"
echo $c2.url2 should print --> "k8s-xyz1.amazonaws.com : 1003"
I have tried like
lenc2=$(cat sample | grep c2|wc -l)
#Get LBs of cluster_2 ###
j=0
while [ $j -lt $lenc2 ]
do
LB=$(cat sample | grep c2.url$j| awk -F: '{print $(NF-1),":",$(NF)}'|sed "s/'/\"/g")
c2.url"$j"=$LB
j=$(( j + 1 ))
done
But while assigning value to variable i am facing issue.
c2.url0=: command not found
c2.url1=: command not found
c2.url2=: command not found
Please help !!
c2.url"$j"=$LBdoes not work. In a variable assignment, you can't have a parameter expansion to the left of the equal sign. Therefore, bash interprets this as a command. You could useeval, or a nameref, or an associative array. In your case, an associative array would perhaps most suitable, but IMO the main problem in your program is that you create 4 child processes for each line of the file sample. You could instead simply loop over the lines of your file and from each line assign your array (no child process needed).