2

I am quite new to bash scripts so take it easy on me;).

I have a file which does some checks before initiating a /warner file which sends an email.

No what the script should do is:

If the word 'rror' is to be found in the log file and if the file is younger then 24 hours > send failed email

If the file is older than 24 hours > send not running error

If none of the above are true and the file is less than 24 hours old and if it contains word 'uccess' > send successful email.

Script it self

string=rror
string1=ucces

FILE=/var/log/rsnapshot.log
OLDTIME=86400
CURTIME=$(date +%s)
FILETIME=$(stat $FILE -c %Y)
TIMEDIFF=$(expr $CURTIME - $FILETIME)

#tail -n0 -F /var/log/rsnapshot.log | \

while read LINE
do
  if [ echo "$LINE" | grep "$string" 1>/dev/null 2>&1 -a $TIMEDIFF -lt $OLDTIME ]; // <- line 18
    then
        /etc/init.d/warner "Warning from Socrates Backup" "Backup Failed"
  elif [ $TIMEDIFF -gt $OLDTIME ];
    then
        /etc/init.d/warner "Warning from Socrates Backup - Backup Not Running!" "Backup file not running"
  elif echo "$LINE" | grep "$string1" 1>/dev/null 2>&1
    then
        /etc/init.d/warner "Message from Socrates" "Backup Sucessfully Completed"
  fi
done </var/log/rsnapshot.log

At present time it does send me a successfull email (because the file is less than 24hours and it does have a 'uccess' message. However are these if / elseif written correctly ? if anything should fail will they get triggered ? and also i am getting this error

line 18: [: missing `]'
2
  • Where is line 18? If I count the lines you pasted, it is in then. Commented Dec 4, 2015 at 14:09
  • @fedorqui Apologies did not see your comment. The 18th line is the first if Commented Dec 4, 2015 at 15:12

1 Answer 1

6

This line is completely wrong:

if [ echo "$LINE" | grep "$string" 1>/dev/null 2>&1 -a $TIMEDIFF -lt $OLDTIME ];

What you mean probably is:

if echo "$LINE" | grep -q "$string" && [ $TIMEDIFF -lt $OLDTIME ];

You want to read Compound Commands from the man bash, while keeping in mind that [ is just a command too. (Try help test.)

Sign up to request clarification or add additional context in comments.

2 Comments

Yup thats the one. i have to do more RTFM
@MaciejCygan, In the beginning, the internal bash's help command was extremely useful to me. The man bash is just too huge to swallow, while for basic syntax the help <cmd> + googling is more than sufficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.