1837195 Members
2473 Online
110115 Solutions
New Discussion

Re: grep in script

 
SOLVED
Go to solution
Ratzie
Super Advisor

grep in script

I am setting up a script that greps for certain words and if they exist, the file is mailed.
But I am having problems setting the script up.

if [ -f $BACKUP_LOG ]
then
if [ `grep -i` '"already exits" | "parameter not set" | "find: cannot get"
| "not found" | "No such file or directory" | "grep:"
| "WARNING" | "FAILED" | "Permission denied"' $BACKUP_LOG ]
then
$MAIL -s "$SUBJECT" $MAILTO < $BACKUP_LOG
fi
fi
16 REPLIES 16
G. Vrijhoeven
Honored Contributor

Re: grep in script

Hi,

I am not able to test but I think that if you replace the grep whit an egrep you are close.

Gideon
Ratzie
Super Advisor

Re: grep in script

I have also tried with egrep...
Sridhar Bhaskarla
Honored Contributor

Re: grep in script

Hi,

If you would like to grep multiple lines, then you would need to use "-E", extended regular expressions. For ex.,

grep -E -i 'already exists|parameter not set' $BACKUP_LOG

etc

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
G. Vrijhoeven
Honored Contributor

Re: grep in script

Hi again,

Sorrie but i can not this but:

....
if [ `egrep -qi 'already exits" | "parameter not set" | "find: cannot get"
| "not found" | "No such file or directory" | "grep:"
| "WARNING" | "FAILED" | "Permission denied"'` ] then
....
etc

Gideon
Ratzie
Super Advisor

Re: grep in script

Tried with -E

if [ `grep -i` '"..."|"..."' file ...

usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
[-f pattern_file...] [file...]
Patrick Wallek
Honored Contributor

Re: grep in script

I would change up your script to something like:

if [ -f $BACKUP_LOG ]
then
VAR1=$(grep -q -i -e "already exists" -e "parameter not set" -e "find: cannot get" -e "not found" -e "No such file or directory" -e "grep:" -e "WARNING" -e "FAILED" -e "Permission Denied" $BACKUP_LOG)
if [ $? = 0 ]
then
$MAIL -s "$SUBJECT" $MAILTO < $BACKUP_LOG
fi
fi

A. Clay Stephenson
Acclaimed Contributor
Solution

Re: grep in script

You need to use "-e" to specify multiple patterns. I would also use -q since all you care about is the status. If 0 then at least one of the patterns was found:

if [[ -f ${BACKUP_LOG} ]]
then
grep -i -E -q -e "already exits" -e "parameter not set" -e "find: cannot get"
-e "not found" -e "No such file or directory" -e "grep:"
-e "WARNING" -e "FAILED" -e "Permission denied" ${BACKUP_LOG}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
${MAIL} -s "${SUBJECT}" ${MAILTO} < ${BACKUP_LOG}
fi
fi

If it ain't broke, I can fix that.
Anil C. Sedha
Trusted Contributor

Re: grep in script

Simple script to do this.

BACKUP_LOG=/tmp/filename
if [ -f $BACKUP_LOG ]; then
grep -i "already exists"|"parameter not set"|"find:cannot get"|"WARNING"| $BACKUP_LOG
if {$? = 0 ]; then
SUBJECT="Error found"
email="admin@test.com"
mailx -s"$SUBJECT" $email<"Errors have been found in the file $BACKUP_LOG"
EOF


Let us know if this helps..

Anil
If you need to learn, now is the best opportunity
Ratzie
Super Advisor

Re: grep in script

I am still having problems, I revamped my script because I like the way you STAT=${?}

But now I get an error...
/home/ac/scripts/backup/d2t_archbu.sh[54]: -e: not found.
/home/ac/scripts/backup/d2t_archbu.sh[55]: -e: not found.


if [[ -f $BACKUP_LOG ]]
then
grep -i -E -q -e "already exits" -e "parameter not set" -e "find: cannot get"
-e "not found" -e "No such file or directory" -e "grep:"
-e "WARNING" -e "FAILED" -e "Permission denied" $BACKUP_LOG
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
$MAIL -s "$SUBJECT" $MAILTO < $BACKUP_LOG
fi
fi



Patrick Wallek
Honored Contributor

Re: grep in script

Your whole grep statement with all the '-e' MUST all be one long line.
Anil C. Sedha
Trusted Contributor

Re: grep in script

In the script i mentioned, i forgot to end it with 2 fi's on seperate line.. pls try using that.
If you need to learn, now is the best opportunity
H.Merijn Brand (procura
Honored Contributor

Re: grep in script

or you can use a file

#!/usr/bin/sh

if [ -f $BACKUP_LOG ]; then
cat >grep.pat <already exits
parameter not set
find: cannot get
not found
No such file or directory
grep:
WARNING
FAILED
Permission denied
EOG
if [ `grep -i -f grep.pat $BACKUP_LOG` ]; then
$MAIL -s "$SUBJECT" $MAILTO < $BACKUP_LOG
fi
rm grep.pat
fi

Easy to read, easy to maintain, easy to extend

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: grep in script

The entire grep command must be one line or continued with the "\" as the LAST character on each line to be combined.
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: grep in script

In response to your "Tried with -E"

$cat data
already exist bla bla
parameter not set yada yada
find: cannot get this is a test line
No such file or directory ofcourse there is no such file or directory
$grep -E -i 'already exist|parameter not set|find: cannot' data
already exist bla bla
parameter not set yada yada
find: cannot get this is a test line

So, it should have worked if you put everything in one line as mentioned by others.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Bill Hassell
Honored Contributor

Re: grep in script

As mentioned, one line is necessary for the grep to work. The HP Forums uses a proportional font and breaks the lines up so they are difficult to interpret. I have taken the approach that long lines are always a problem (cut-n-paste, folded by editors, etc) so i keep them short by escaping the end-of-line and putting multiple options on separate lines. This has the added effect of making the actual command much more readable:

if [ grep -i
-e "already exits" \
-e "parameter not set" \
-e "find: cannot get" \
-e "not found" \
-e "No such file or directory" \
-e "grep:" \
-e "WARNING" \
-e "FAILED" \
-e "Permission denied" \
$BACKUP_LOG ]
then
...
fi

Note that -E (or egrep) brings a lot of extra parsing code into play for extended regular expressions. Usually the -e approach is faster and easier to read. You might want to change the words WARNING and FAILED to WARN and FAIL so grep will pickup both forms. And I would add ERROR and CRITICAL to your list just in case.


Bill Hassell, sysadmin
Bill Hassell
Honored Contributor

Re: grep in script

Note that the first line of my example is missing a trailing \ at the end. The \ removes the special meaning of the end-of-line and makes all the lines look like one.


Bill Hassell, sysadmin