Operating System - HP-UX
1821410 Members
2541 Online
109633 Solutions
New Discussion юеВ

Re: shell script - command grep

 
SOLVED
Go to solution
Jairo Campana
Trusted Contributor

shell script - command grep

Hello :

I have folloging problem in script with grep:

/usr/bin/rsh 10.1.1.10 /usr/bin/grep camaro /opt/cars/log/chevrolet.log
echo $?
0
Its is Ok "camaro" exist in chevrolet.log

my problem is when execute grep

/usr/bin/rsh 10.1.1.10 /usr/bin/grep nissan /opt/cars/log/chevrolet.log
echo $?
0

the result is 0 , but word nissan no exist in chevrolet.log

the result it would have 1

as it can be the problem.

thanks
legionx
5 REPLIES 5
RAC_1
Honored Contributor
Solution

Re: shell script - command grep

In both cases, the return status is of rsh command and not of grep command. (which is being executed on other host)

You will have to redirect the exit status of grep to some file and then read it seperately. Something like follows.

/usr/bin/rsh 10.1.1.10 "/usr/bin/grep camaro /opt/cars/log/chevrolet.log;echo $? > /tmp/test" ; /usr/bin/rsh 10.1.1.10 "cat /tmp/test"

Anil
There is no substitute to HARDWORK
Pete Randall
Outstanding Contributor

Re: shell script - command grep

I suspect it is giving the result of the rsh command rather than your grep command.


Pete

Pete
Jairo Campana
Trusted Contributor

Re: shell script - command grep

thanks ; but
replace " for '

/usr/bin/rsh 10.1.1.10 '/usr/bin/grep camaro /opt/cars/log/chevrolet.log;echo $? > /tmp/test' ; /usr/bin/rsh 10.1.1.10 'cat /tmp/test'

legionx
Biswajit Tripathy
Honored Contributor

Re: shell script - command grep


/usr/bin/rsh 10.1.1.10 '/usr/bin/grep camaro /opt/cars/log/chevrolet.log;echo $?'

- Biswajit
:-)
harry d brown jr
Honored Contributor

Re: shell script - command grep


If you only want to know if the value is in the file and you don't care to see the line it is on, then use the -q option of grep, like this:

xyz=`remsh 10.1.1.10 'grep -q nissan /opt/cars/log/chevrolet.log;echo $?'`

echo $xyz|sed "s/\(.*\)\([0-9]\)$/\1/"

If you want to see the "output" and the "return code" then use this:

xyz=`remsh 10.1.1.10 'grep nissan /opt/cars/log/chevrolet.log;echo $?'`

This displays the output of the grep:
echo $xyz|sed "s/\(.*\)\([0-9]\)$/\1/"

This displays the output of the "return code":
echo $xyz|sed "s/\(.*\)\([0-2]\)$/\2/"

grep only has three return codes (see man grep) 0, 1 and 2.

live free or die
harry d brown jr
Live Free or Die