Unix Script to email grepped results

I am following the tread number 81556 to grep a file and send the results in email.

#/bin/bash
/bin/grep -i 'invite sip' /var/log/asterisk/full 
if [ $? -eq 0 ] echo "found" |
mail -s 'invite sip' mail@gmail.com

When I just build it to grep and mail, it works fine. However, the if statement causes the script to error out. I get the following error:

./grep_email.sh: line 5: syntax error: unexpected end of file 

I searched high and low and am stuck on this simple task. Any help is appreciated.

Unexpected EOF is because it's looking for a then to match the if statement.

Try this (changes in green)

#/bin/bash
/bin/grep -i 'invite sip' /var/log/asterisk/full 
if [ $? -eq 0 ] 
then
    echo "found" | mail -s 'invite sip' mail@gmail.com
fi
1 Like

yea you gotta keep to the if/then or if/then/else...syntax. You need to specify what to do in the event the test (if) is true or false. Human speak: If something evaluates to true or false then do something in that event 'or else do something different'. If must be terminated by fi. If in fi out.

Thank you! I tried the THEN statement but I guess it can't be on the same line as the IF statement. Next I will figure out how to include the results and have it pick up off where it left off on an active log.

Thank you.

If you want to do a simple test on the one line just drop the if like this:

[ $? -eq 0 ] && echo found | mail -s 'invite sip' mail@gmail.com