Register
It is currently Sat Jul 26, 2014 1:13 pm

Bash is not a friend of mine...


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Tue Jun 21, 2011 1:10 pm   
User avatar

Joined: Tue Jun 21, 2011 12:41 pm
Posts: 5
Location: San Antonio, TX
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. O:-)

v/r,

Mia-


Top
 Profile  
 PostPosted: Tue Jun 21, 2011 2:30 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Does this help you?
Code:
OIFS=$IFS
IFS=':' # fields now separated by ;
read -a array <<<"RULE-NAME123:OUTBOUND:::DENYALL:192.168.1.0:255.255.255.0:TCP:;"
unset array[8] # remove the last ;
for member in "${array[@]}" ; do
  echo $member
done
IFS=$OIFS


Top
 Profile  
 PostPosted: Tue Jun 21, 2011 2:45 pm   
User avatar

Joined: Tue Jun 21, 2011 12:41 pm
Posts: 5
Location: San Antonio, TX
choroba, :)

Sweet, thank you so much I also fixed the array issues I was having I changed it to multi-d array like this:

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"
      declare -A ruleMaster
      ruleMaster[$numelement,$numline]=$element
      numelement=$[$numelement+1]
   done
done



I'll mesh them together and post a completed solution.

Many Props!! :))

v/r,

Mia-


Top
 Profile  
 PostPosted: Tue Jun 21, 2011 3:14 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Code:
#!/bin/bash

str='RULE-NAME123:OUTBOUND:::DENYALL:192.168.1.0:255.255.255.0:TCP:;'

read -ra unix1 <<< "${str//[:;]/ }"

for i in "${!unix1[@]}"; do
    printf 'unix1[%d] = %s\n' $i "${unix1[i]}"
done


Top
 Profile  
 PostPosted: Tue Jun 21, 2011 3:49 pm   
User avatar

Joined: Tue Jun 21, 2011 12:41 pm
Posts: 5
Location: San Antonio, TX
jsz,

Thank you as well. O:-) With all these solutions I cannot Fail.....perhaps. @-)

I'll post a meshed solution tomorrow.

v/r,

Mia-


Top
 Profile  
 PostPosted: Wed Jun 22, 2011 8:59 am   
User avatar

Joined: Tue Jun 21, 2011 12:41 pm
Posts: 5
Location: San Antonio, TX
Hello all,
Thank you for all the help.
Here is what I have:
Code:
numline=0
numelement=0

ruleSet=$(echo $my_rules | tr ";" "\n")
for ruleline in $ruleSet; do
        echo "$ruleline"
        rule=$(echo $ruleline | tr ":" " \n")
        OIFS=$IFS
        IFS=':' # fields now separated by ;
        read -ra ruleMaster <<< "${ruleline//;/ }"
        numelement=0
        for element in  "${!ruleMaster[@]}"; do
                echo "${ruleMaster[element]}"
                declare -A ruleSetMaster
                ruleSetMaster[$numline,$numelement]=${ruleMaster[element]}
                numelement=$[$numelement+1]
        done
        IFS=$OIFS
        numline=$[$numline+1]
done

Excellent, this is the only other change I have made to the data::
Quote:
"RULE-NAME123:OUTBOUND:::DENYALL:192.168.1.0:255.255.255.0:TCP:;RULE-NAME124:OUTBOUND:::DENYALL:192.168.1.0:255.255.255.0:TCP:6338-6339;"

With this output:
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

RULE-NAME124:OUTBOUND:::DENYALL:192.168.1.0:255.255.255.0:TCP:6338-6339
RULE-NAME124
OUTBOUND
DENYALL



193.168.1.0
255.255.255.0
TCP
6338-6339


v/r,

Mia-


Last edited by miazma on Wed Jun 22, 2011 9:45 am, edited 2 times in total.

Top
 Profile  
 PostPosted: Wed Jun 22, 2011 9:05 am   
User avatar

Joined: Tue Jun 21, 2011 12:41 pm
Posts: 5
Location: San Antonio, TX
Hello again,

I just wanted you all to see that the data is now referenced like this:
Quote:
echo ${ruleSetMaster[0,0]}
echo ${ruleSetMaster[1,0]}
echo ${ruleSetMaster[2,0]}
echo ${ruleSetMaster[3,0]}

With an output of:
Quote:
RULE-NAME123
RULE-NAME124
RULE-NAME125
RULE-NAME126

Nice Multi-Dimensional Array reference for everyone.

Mia- :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP