Operating System - HP-UX
1820694 Members
2612 Online
109627 Solutions
New Discussion юеВ

grep > file in ksh script and set-e

 
SOLVED
Go to solution
Franky Leeuwerck
Frequent Advisor

grep > file in ksh script and set-e

I have a ksh script running in a batch job scheduling application.

It looks like this :

set -e
..
grep -i licens /tmp/licmessages > /tmp/licwarns
..
set +e

The script needs to scan the 'licmessages' file for the word 'licens'. If the word 'licence' doesn't occur. The script execution stops (because of the set -e statement). All scripts on the application use this set-e and set+e statements for avoiding job scripts from hanging.

Eliminating the set-e,set+e statements doesn't force the script to quit when an error occurs. The job executes nicely the rest of the statements. However, this is not the standard way of using the application.

How can I catch the error in the the grep statement and still use the set-e,set+e statements ?

Thanks

Franky

4 REPLIES 4
Carlos Fernandez Riera
Honored Contributor

Re: grep > file in ksh script and set-e

From man grep:



RETURN VALUE
Upon completion, grep returns one of the following values:

0 One or more matches found.
1 No match found.
2 Syntax error or inaccessible file (even if matches were
found).

____


Grep will usualy return a not 0 value. You can not use set +e while using grep.



unsupported
Steve Steel
Honored Contributor
Solution

Re: grep > file in ksh script and set-e

Hi


try

if grep -i licens /tmp/licmessages > /tmp/licwarns
then
it worked
else
it did not work
fi


This should not be affected by set -e


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Paula J Frazer-Campbell
Honored Contributor

Re: grep > file in ksh script and set-e

Frankie

How about something like this:


-----------------------------
#!/bin/sh
####################
# Lic Check
# How many
count=`cat /tmp/licmessages | grep licen | wc -l`
# Show result
echo $count
# Use the result to do somthing
if [ $count != 0 ]
then
echo "More than zero"
fi
---------------------------------
HTH

Paula

If you can spell SysAdmin then you is one - anon
Franky Leeuwerck
Frequent Advisor

Re: grep > file in ksh script and set-e

Thanks all for the quick replies and explanations.
I used Steves solution.
It worked !
Regards,

Franky