-1

Can anybody tell me what's wrong in this script, it's not working. When I run it, there is no output/error on the screen.

The script is to monitor a log file to check the value of one of the columns, and if it is more than 20 it will echo a message.

#!/bin/bash

while true ; do

COUNT=`tail -f /monitoring/log.20160121|cut -d" " -f39`

echo $COUNT

if [ $COUNT -gt 20 ] ;then

echo "Count is high"

break

fi

sleep 10

done
Unixhelp
  • 7
  • 1

1 Answers1

3

tail -f does not exit, so your script gets stuck there. I assume you are just interested in the last line of the log; tail -n 1 does that.

Other points:

  • Indentation: not sure how much got lost while copy pasting, but proper indentation massively increases readability of your code
  • Variable names: all uppercase variable names are discouraged as they might clash with reserved (environment) variable names
  • Command substitution with backticks (` `) is discouraged and the form $( ) is preferred; makes for example nesting easier
  • Since you're using Bash, you can use the (( )) conditional construct, which is better suited for comparing numbers than [ ]

Together:

#!/bin/bash

while true; do
    count=$(tail -n 1 /monitoring/log.20160121 | cut -d " " -f 39)

    echo $count

    if (( count > 20 )); then
        echo "Count is high"
        break
    fi

    sleep 10

done
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • Thanks Benjamin for your expert comments,that will help.But with tail -n,script just exits after checking last line but i want to continuously monitor this log file – Unixhelp Jan 22 '16 at 04:34
  • @Unixhelp Well, it checks the last line of the log file every ten seconds, and if field 39 is larger than 20, it breaks out of the while loop. It shouldn't exit just after reading from the file. – Benjamin W. Jan 22 '16 at 06:34