user11975370
user11975370

Reputation: 1

for loop through array always starts from element 0, instead of next element

for some reason, my for loop is iterating through my array, but always starting from from element 0 during each loop.

PROBLEM RESULTS: iterations repeat from .10 IP on each cycle:

 192.168.0.10  # iteration 1     
 conf=/path/to/company/app/server001     
 -----     
 192.168.0.10  # iteration 2     
 conf=/path/to/company/app/server001    
 -----     
 192.168.0.20    
 conf=/path/to/company/app/cluster    
 -----    
 192.168.0.10  # iteration 3    
 conf=/path/to/company/app/server001     
 -----     
 192.168.0.20     
 conf=/path/to/company/app/cluster    
 -----    
 192.168.0.30    
 conf=/path/to/company/app/server003    
 -----    
 192.168.0.10 # iteration 4    
 conf=/path/to/company/app/cluster     
 -----    
 192.168.0.20    
 conf=/path/to/company/app/cluster     
 -----    
 192.168.0.30    
 conf=/path/to/company/app/server003     
 -----     
 192.168.0.40    
 conf=/path/to/company/app/server004     
 -----       

if I remove the 'if statements', and just echo the "${rfsconfig[@]}" the results seem to come out correct, BUT... I need the if statments to grep for contents in the conf=/path/to/company/app/. not sure why that would cause a problem.

 declare -a ipcheck=(     
 "192.168.0.10"      
 "192.168.0.20"      
 "192.168.0.30"      
 "192.168.0.40"      
 )     
 delcare -a rfsconfig=()     
 for ip in "${ipcheck[@]}"; do      
     rfsconfig+=($(awk "/$ip/"'{print $0; getline; getline; print $0; print "-----"}' /home/iaw/D1/kmdata/config/config))                
     for i in "${rfsconfig[@]}"; do              
         if [[ $i =~ ^remote* ]] ; then              
             echo $i     
         elif [[ $i =~ ^native_path* ]] ; then     
             echo $i     
             npath=${i#*=}     
             ls $npath | grep key           
          else      
              echo $i      
          fi     
        done     
     done     

DESIRED RESULTS

192.168.0.10    
conf=/path/to/company/app/server001    
-----    
192.168.0.20    
conf=/path/to/company/app/cluster    
-----    
192.168.0.30    
conf=/path/to/company/app/server003    
-----    
192.168.0.40    
conf=/path/to/company/app/server004

Upvotes: 0

Views: 70

Answers (2)

oliv
oliv

Reputation: 13249

Parsing text file is easier using sed or awk rather than bash scripting:

Here is an example of getting unique record containing ip and path of your input file using GNU awk:

awk 'BEGIN{RS="----- *\n"; OFS="\n"; ORS="\n-----\n"}
     !a[$1]{a[$1]=$2}
     END{for(i in a) print i,a[i]}' file

This script assumes the comment # iteration x is not part of the your input file.

The BEGIN statement sets the variables to parse and display multi line records.

The second statement fills the array a with both ip and path.

The END statement prints the content of the array a.

Upvotes: 0

root
root

Reputation: 6038

You're always appending to rfsconfig, so things left from the .10 IP are always there and so you'll always iterate over them.

Change rfsconfig+=(...) to rfsconfig=(...) so that the iteration on rfsconfig doesn't include things from the .10 IP.

Upvotes: 1

Related Questions