I wrote the following code:
success="Traffic Engineering Tunnel validated successfully"
text=$(tail -2 /apps/pofitb/logs/tunnels.log)
echo $executed_date
if [[ $text = $success ]]
then
text=$(tail -2 /apps/pofitb/logs/tunnels.log)
echo "Tunnel script execution is successful" | mailx -s "Tunnel script is executed succefsfully on $executed_date" abc@gmail.com
else
echo "Tunnel script lastly executed on $executed_date" | mailx -s "Tunnel script FAILED!!!" abc@gmail.com
fi
exit
Currently tunnel.log file has blank line while being updated. So, text=$(tail -2 /apps/pofitb/logs/tunnels.log)
extracts the last non-blank line from the end of the file. This works if the number of blank line inserted at the end of the file is 1.
How can I modify the script such that the script searches for the last last non-blank line from the file tunnel.log, irrespective of the number of blank lines inserted ?
awk to the rescue!
tac log | awk 'NF{print;exit}'
if your log is too long, start with a generous tail first
tail -5 log | tac | awk 'NF{print;exit}'
will print the last non-empty line.