vendredi 4 décembre 2015

Bash script - if and else if statements

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 `]'

Aucun commentaire:

Enregistrer un commentaire