1832763 Members
3094 Online
110045 Solutions
New Discussion

Re: script help

 
wanaka_1
Occasional Advisor

script help

PATTERN="HTL_"
RESULT=$(cat $Log | grep $PATTERN)

I look for the pattern in the log file. How I can return true and false. Let say if I can't find any it is false otherwise it is true.
3 REPLIES 3
Michael_356
Frequent Advisor

Re: script help

Hi there,

use wc:

RESULT=0

PATTERN="HTL_"
RESULT=`cat $Log | grep $PATTERN|wc -l`

if [$RESULT -gt 0 ]; then
.
.
.
fi

Regards

Michael
John Palmer
Honored Contributor

Re: script help

If you're not concerned about getting the actual lines containing $PATTERN into the RESULT variable, you can do this...
if grep -q ${PATTERN} ${Log}
then
else
fi

If you do want RESULT to contain the lines...
RESULT=$(grep ${PATTERN} ${Log})
if [[ ${?} -eq 0 ]];
then
else
fi

Regards,
John
Muthukumar_5
Honored Contributor

Re: script help

We can do this simply as,
PATTERN="HTL_"
grep "$PATTERN" -q $log
if [[ $? -eq 0 ]]
then
echo "$PATTERN is there in $log file"
else
echo "$PATTERN is not there in $log file"
fi

-q will do the action in silent mode and return result there.

RESULT=$(cat $Log | grep $PATTERN) --> It will do that RESULT variable will contain the $(cat $Log | grep $PATTERN) execution informations'
Easy to suggest when don't know about the problem!