Prev: [announcement] runawk-0.21.0 -- yet another power tool to program in shell
Next: AWK - Stop processing
From: Chris on 1 Apr 2010 06:53 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
From: Bill Marcum on 1 Apr 2010 07:49 On 2010-04-01, Chris <cconnell_1(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 > The command "echo $test1" returns 0. If you want to use $? from the pipe, you have to get it before the echo. If you use bash, you can check ${PIPESTATUS[*]} to see whether snmpwalk or grep failed. -- Alexander Hamilton started the U.S. Treasury with nothing - and that was the closest our country has ever been to being even. -- The Best of Will Rogers
From: Chris on 1 Apr 2010 09:15 On Apr 1, 1:49 pm, Bill Marcum <marcumb...(a)bellsouth.net> wrote: > On 2010-04-01, 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 > > The command "echo $test1" returns 0. If you want to use $? from the > pipe, you have to get it before the echo. If you use bash, you can > check ${PIPESTATUS[*]} to see whether snmpwalk or grep failed. > > -- > Alexander Hamilton started the U.S. Treasury with nothing - and that was > the closest our country has ever been to being even. > -- The Best of Will Rogers Thanks, I checked pipe status, is there a way for grep to return status code 0 if the the string is "not" found? i.e. grep in reverse.
From: Jean-Rene David on 1 Apr 2010 10:08 * Chris [2010.04.01 13:15]: >> > 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? Not sure this counts as one command: x="$(snmpwalk -v2c yadayada...)" && \ if printf "$x" | grep -q myalarm ; then /bin/false ; fi This will return the status code of "snmpwalk" if "snmpwalk" fails, or 1 (the return value of /bin/false) if "myalarm" is found in the output of "snmpwalk". Maybe others can improve it. Hope this can get you started. -- JR
From: Thomas 'PointedEars' Lahn on 1 Apr 2010 10:51 Chris wrote: > Bill Marcum wrote: >> Chris wrote: >> > 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? >> > [...] >> >> The command "echo $test1" returns 0. If you want to use $? from the >> pipe, you have to get it before the echo. If you use bash, you can >> check ${PIPESTATUS[*]} to see whether snmpwalk or grep failed. >> [...] > > Thanks, I checked pipe status, is there a way for grep to return > status code 0 if the the string is "not" found? i.e. grep in reverse. Yes. And trim your quotes, please. <http://www.catb.org/~esr/faqs/smart-questions.html> PointedEars
|
Next
|
Last
Pages: 1 2 Prev: [announcement] runawk-0.21.0 -- yet another power tool to program in shell Next: AWK - Stop processing |