From: Ed Morton on
On 4/1/2010 5:53 AM, Chris wrote:
> HI
> Im trying to get this condition to work.
>
> If snmpwalk command works correctly (i.e. returns status code of 0 -
> community string ok etc) and does not return a string (i.e. matching
> alarm I want from grep) then return either success code or a certain
> string I can use in my condition/action.
>
> I have tried
>
> test1=`snmpwalk -v2c -c public localhost 2>&1 | grep "myalarm"`
> echo $test1
>
> But the status code returns fail (i.e. return 1) if snmpwalk doesnt
> work (which I want), but return a success if the string is either
> found or not found. I want to have
>
> return 1 if either command does not work or string is found.
> return 0 if command works and matching string not found.

By "return 1" do you mean "exit with status 1" or "print the number 1"?

> Is there a way to do this in one command?
> Cheers

Why do you care about the exit status? It sounds like you really only care about
the value of "test1".

Ed

From: Rakesh Sharma on
On Apr 1, 3:53 pm, Chris <cconnel...(a)lycos.com> wrote:
> HI
> Im trying to get this condition to work.
>
> If snmpwalk command works correctly (i.e. returns status code of 0 -
> community string ok etc) and does not return a string (i.e. matching
> alarm I want from grep) then return either success code or a certain
> string I can use in my condition/action.
>
> I have tried
>
> test1=`snmpwalk -v2c -c public localhost 2>&1 | grep "myalarm"`
> echo $test1
>
> But the status code returns fail (i.e. return 1) if snmpwalk doesnt
> work (which I want), but return a success if the string is either
> found or not found. I want to have
>
> return 1 if either command does not work or string is found.
> return 0 if command works and matching string not found.
>
> Is there a way to do this in one command?
> Cheers



CMD='snmpwalk -v2c -c public localhost'; # define the command to be
run

# suppress the errors from command; suppress stdout+stderr from grep
since we only care about pass/fail status.
${CMD} 2> /dev/null | grep -l 'myalarm' 1> /dev/null 2>&1; # note no
quoting the CMD variable

case $PIPESTATUS[0] in
0 )
# snmp command passed
case $? in
0 ) STATUS=1;; # grep passed => string found
* ) STATUS=0;; # grep failed => string not foud
esac
;;

* )
# snmp command failed, now dont care about the status of grep
STATUS=1
;;
esac

exit "$STATUS"



-- Rakesh