Hello all,
This is my first post so please be gentile.
I need to parse a stream/string with 10 elements separated by colon and each line ends with a semi-colon.
Quote:
"RULE-NAME123:OUTBOUND:::DENYALL:192.168.1.0:255.255.255.0:TCP:;"
It represents a firewall rule. Here is what I have so far.
Code:
numline=0
numelement=0
ruleSet=$(echo $my_rules | tr ";" "\n")
for ruleline in $ruleSet
do
numline=$[$numline+1]
echo "$ruleline"
rule=$(echo $ruleline | tr ":" "\n")
for element in $rule
do
echo "$element"
unix$numline[$numelement]=$element
numelement=$[$numelement+1]
done
done
So I'm adding an element to an array for every rule line added. The output is this:
Quote:
RULE-NAME123:OUTBOUND:::DENYALL:192.168.1.0:255.255.255.0:TCP:
RULE-NAME123
OUTBOUND
DENYALL
193.168.1.0
255.255.255.0
TCP
But the Array is in error. When I echo the array statement is looks good:
Code:
numline=0
numelement=0
ruleSet=$(echo $my_rules | tr ";" "\n")
for ruleline in $ruleSet
do
numline=$[$numline+1]
echo "$ruleline"
rule=$(echo $ruleline | tr ":" "\n")
for element in $rule
do
echo "$element"
echo unix$numline[$numelement]=$element
numelement=$[$numelement+1]
done
done
With this output, Looks GOOD!:
Quote:
RULE-NAME123:OUTBOUND:::DENYALL:192.168.1.0:255.255.255.0:TCP:
RULE-NAME123
unix1[0]=RULE-NAME123
OUTBOUND
unix1[1]=OUTBOUND
DENYALL
unix1[2]=DENYALL
193.168.1.0
unix1[3]=193.168.1.0
255.255.255.0
unix1[4]=255.255.255.0
TCP
unix1[5]=TCP
Yet this is my current output without the echo:
Quote:
RULE-NAME123:OUTBOUND:::DENYALL:192.168.1.0:255.255.255.0:TCP:
RULE-NAME123
test.sh: line 145: unix1[0]=RULE-NAME123: command not found
OUTBOUND
test.sh: line 145: unix1[1]=OUTBOUND: command not found
DENYALL
test.sh: line 145: unix1[2]=DENYALL: command not found
193.168.1.0
test.sh: line 145: unix1[3]=193.168.1.0: command not found
255.255.255.0
test.sh: line 145: unix1[4]=255.255.255.0: command not found
TCP
test.sh: line 145: unix1[5]=TCP: command not found
1) What did I do wrong, array not being built?
2) Why aren't the missing rules displayed. Meaning the :::: is still an element, it contains "" but still an element in the array. I would expect it to have a successful output of this:
Quote:
RULE-NAME123:OUTBOUND:::DENYALL:192.168.1.0:255.255.255.0:TCP:
RULE-NAME123
OUTBOUND
DENYALL
193.168.1.0
255.255.255.0
TCP
Giving me an array filled like this:
Quote:
unix1[0]=RULE-NAME123
unix1[1]=OUTBOUND
unix1[2]=
unix1[3]=
unix1[4]=
unix1[5]=DENYALL
unix1[6]=193.168.1.0
unix1[7]=255.255.255.0
unix1[8]=TCP
unix1[9]=
Any help would be appreciated.
v/r,
Mia-