0

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
4
  • 2
    The code contains syntax errors and there is no question.
    – jordanm
    Commented Jun 5, 2013 at 15:50
  • You have to put spaces around command names such as [ (and the [ command expects its last argument to be ], complaining if it is not). Commented Jun 5, 2013 at 16:03
  • 1
    For bash, it's better to use [[ and ]], which is smarter but less portable. You'll have fewer syntax errors, and less confusing behavior that way.
    – jpaugh
    Commented Jun 5, 2013 at 16:19
  • For incrementing, use either let ARRAY_COUNTER=$ARRAY_COUNTER+1 or ((ARRAY_COUNTER++)) Commented Jun 5, 2013 at 20:23

1 Answer 1

1
  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.
5
  • Why is seq (an executable) preferable to a built-in notation? Is it because {$min..$max} doesn't work? Or something else?
    – jpaugh
    Commented Jun 5, 2013 at 16:21
  • Also, I usually recommend [[ as a catch-all syntax "correction" to [ -- at least for bash.
    – jpaugh
    Commented Jun 5, 2013 at 16:22
  • @jpaugh Consider you'd like to vary the step, i.e. 0, 2, 4, ... 32. I'm not a BASH expert. Is it possible to do it in {...}? Thanks. Commented Jun 5, 2013 at 16:39
  • @TimEagle You're right. The leading 0 is important in the code. I missed. Commented Jun 5, 2013 at 16:40
  • @liuml07 No, you can't adjust the step. Then again, I've never tried! :-)
    – jpaugh
    Commented Jun 5, 2013 at 16:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.