Operating System - HP-UX
1849034 Members
4375 Online
104041 Solutions
New Discussion

Re: Shell scripts to grep and compare strings ?? Any idea ?

 
Chris Fung
Frequent Advisor

Shell scripts to grep and compare strings ?? Any idea ?

Dear all,

Shell scripts like other programs should have error handling routine that in some cases help generating error messages and error codes for bug fixing and trouble shooting. Now I would like to grep some of the error code and error messages generated by the application program and compare the error messages with a configuration file with pre-defined string pattens.

However, I have difficulties in implementing a simple loop to handle all the possible string pattens. Sometimes I need to:
1. grep "xxx" filename
2. grep "xxx" filename | grep -vh "yyy"
3. grep "xxx" filename | grep -v "yyy" |head -1
4. grep "xxx" filename | sort -u

....etc.

I have try to incorporate the whole command set into the configuration file, but was in vain !! Any suggestions?? Appreciated it if anyone can help!!

Many thanks,

Chris,
8 REPLIES 8
Justo Exposito
Esteemed Contributor

Re: Shell scripts to grep and compare strings ?? Any idea ?

Hi Chris,

I have a few questions:
1.- xxx and yyy are error codes and reside in a different file or you input at runtime?
2.- How you determine what option to do?
3.- The filename is fixed or depends?

Regards,

Justo.
Help is a Beatiful word
Gnananandhan
Frequent Advisor

Re: Shell scripts to grep and compare strings ?? Any idea ?

Give more information about exact phrase.

Gnana A.
If there is a better way to do it, find it !
harry d brown jr
Honored Contributor

Re: Shell scripts to grep and compare strings ?? Any idea ?


Chris,

look at egrep or better yet fgrep.

egrep AKA "grep -e string -e string ..." allows you to put many things on one line.

fgrep AKA "grep -f fine_name_containing_patterns" allows you to have a file of patterns to search for.

If you "man grep" you will see these options.

But what you really need is perl, which will give you more than enough ability to "trap" errors. Shell scripting (ksh, csh, bash, ...) is for simple things. The more complicated tasks should be written in perl, although I even write the simple things in perl.

live free or die
harry
Live Free or Die
Mark Landin
Valued Contributor

Re: Shell scripts to grep and compare strings ?? Any idea ?

Seconded on the Perl. My first approximation of a Perl solution: load the predefined strings from the config file into a hash, then look up each input error code to see if there is a hash entry for it. There may be other methods (in fact a Perl axiom is "there is more than one way to do it") but this is the first quick-and-dirty thing that came to mind.

Tim D Fulford
Honored Contributor

Re: Shell scripts to grep and compare strings ?? Any idea ?

I'm not sure I understand the problem, but...

The "return code" from a command string will be that of the LAST command [from a string of commands piped together "|" ]

e.g.

grep abc file1 | egrep -v x | grep a

If the second grep (grep -v x) fails the rc=1 but the last grep could then succeed and the rc=0. (I'm not 100% sure if grep -v can fail, but assume it can)

To get the return code from a string use $? variable i.e.

#!/usr/bin/ksh
grep abc file1
RetCode=$?
if [ $RetCode -eq 0 ]
then
echo "the cmd worked"
else
echo "the cmd failed***"
fi

I hope this helps

Tim
-
Chris Fung
Frequent Advisor

Re: Shell scripts to grep and compare strings ?? Any idea ?

I think I need to go for perl !! Any Good resources on the web?? Sample codes??

Thanks,

Chris
Rodney Hills
Honored Contributor

Re: Shell scripts to grep and compare strings ?? Any idea ?

That question has been asked many times at this forum. Do a forum search on perl resources and you should find some good information.

-- Rod Hills
There be dragons...
Ameet_HP
Frequent Advisor

Re: Shell scripts to grep and compare strings ?? Any idea ?

Do as Tim shown, only put Error Codes in it..

If [ $? = 0 ] ;then
echo "Command Passed"
RC=0
else
echo "Comand failed"
RC=1
fi

You can add more grep commands under failure, i.e. after Command Failed & debug with different greps & thus change the value of RC in it. This way your will get perfect Return Code, RC.

Ameet