Tim Eagle
Tim Eagle

Reputation: 51

BASH Loop through multiple arrays

So, I have an array of IP's, and an Array of Dates... Both Arrays are the same length So, DATE[0] is the date that IP[0] was assigned...

I'm trying to parse through an entire month of logs and change the IP when it hits a certain date... I know this isn't right, so please help with my code: (the Grep statements DO work, are from other code, basically just need to change SEARCHPATH depending on the date check....)

ARRAY_COUNTER=0

NEW_GREP_TERM=${IPS[0]}

for i in {01..31}
do
SEARCHPATH=${BASEPATH}/${DEF_YEAR}${DEF_MONTH}/SG_22[8-9]${DEF_MONTH}${i}*
zgrep --no-filename $NEW_GREP_TERM $SEARCHPATH | awk -f /usr/local/bin/cvsit.awk >> $OUTFILE 
 if [$i = ${DATES[$ARRAY_COUNTER]}]
        then
        NEW_GREP_TERM = ${IPS[$ARRAY_COUNTER]}
        zgrep --no-filename $NEW_GREP_TERM $SEARCHPATH | awk -f /usr/local/bin/cvsit.awk >> $OUTFILE 
        ARRAY_COUNTER=$ARRAY_COUNTER+1

fi
done

Upvotes: 0

Views: 276

Answers (1)

Mingliang Liu
Mingliang Liu

Reputation: 5787

  1. As Jonathan pointed out, the [ and ] should have leading and post blank(s). You can run which [ to know that the [ is actually a command. The if keyword accepts the boolean statement or variable, and the [ foo ] is to test the foo to be true or false.
  2. When you would like a range of integers like 0, 1, 2, 3, ..., 31, you can use $(seq 0 31) in BASH. EDIT: The leading 0 is important here. Thus the {...} built-in notation is more preferable.
  3. It's better to make the indent consistent. It's more human-readable.

Upvotes: 1

Related Questions