Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need help with this script This script analyses apache_access.log through a filter.txt, if exist the term in filter the script will do something. My problem i think is in the first command "for", I am not able to use the filter line by line.

can someone help me?

for term  in `tail -n $LINESTOSEARCH $IPEATERFILTER`
do
    for ip in `tail -n $LINESTOSEARCH $LOGFILE | grep "$TERM" | awk "{print \\$1}" | sort | uniq -c | sort -rn | head -20 | awk "{if (\\$1 > $THRESHOLD) print \\$2}"`
    do
        # Look in iptables to see if this IP is already banned
        if ! iptables -L INPUT -n | grep -q $ip
        then



     # Do something



 fi

    done
done
share|improve this question

1 Answer 1

up vote 1 down vote accepted

You don't want to read lines with for. Use while and read instead. The problem is that the shell will split arguments on whitespace, so any space in the log file's line will actually cause the loop to run another time.

tail -n $LINESTOSEARCH $IPEATERFILTER | while read -r line; do
    # do something with "$line"
done

Repeat the same pattern for inner loops.

share|improve this answer
    
Thank you for explanation. Your command only read the first line in filter. –  Rui Branco Jul 9 at 14:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.