Jairochanchun
Jairochanchun

Reputation: 5

Read last lines from log using tail in a bash script

The objective of this script is to read the last 1000 lines of the log file. If the word "error" exists, show the affected line.
I used the tail command because the log file is updated every second.

I can not include the tail command inside a variable.

word="error -"
find1=`tail -n 1000 /logs/console.log | grep "$word" | awk '{print $A1}' `
#
#
if echo "$find1" | grep -q "$word"; then echo "Error: $find1"
        exit 1;
else echo "No errors"
        exit 0;
fi;

I want to have a exit variable updated all the time.

Upvotes: 0

Views: 4966

Answers (1)

Maayana
Maayana

Reputation: 76

How about

if tail -n 1000 /logs/console.log | grep "$word" | awk '{print $A1}'; then 
        exit 1;
else echo "No errors"
        exit 0;
fi;

This will print the result during the if statement if the word exists and exit

Not sure what you mean by "I want to have a exit variable updated all the time." exit is a command - it will exit the script gracefully if the word was not found (exit 0), and it will exit "with errors" if the word is found (exit 1)

Upvotes: 2

Related Questions