Want to terminate command execution when string found in the command output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Want to terminate command execution when string found in the command output
# 1  
Old 10-10-2012
Want to terminate command execution when string found in the command output

Hi Experts,

I am very much new to linux scripting, I am currently working on reducing my manual work and hence writing a script to automate few task.

I am running below command to snmpwalk the router..

Code:
snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv 182.19.96.13 |grep ae0.784

get below output..

Code:
Did not find 'MplsVpnName' in module MPLS-VPN-MIB (/usr/local/share/snmp/mibs/mib-jnx-ldp.txt)
IF-MIB::ifDescr.2500 = STRING: ae0.784
Timeout: No Response from 182.19.96.13

this command finds the string and show it in output..but when I run below command..

Code:
snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv 182.19.96.13 |grep ae0.784 |awk '{print $2}'

get below output..

Code:
Did not find 'MplsVpnName' in module MPLS-VPN-MIB (/usr/local/share/snmp/mibs/mib-jnx-ldp.txt)
Timeout: No Response from 182.19.96.13
=

command is verymuch fine..but i am not getting why it shows timeout when i try to print 2 column...

also one more requirement is program should terminate once match is found and also print given column in some output file..

looking for help from the experts...

thanks in advance!!!

Regards,
Hanuamnt

Last edited by Scrutinizer; 10-10-2012 at 07:47 AM.. Reason: code tags
# 2  
Old 10-10-2012
Code:
Did not find 'MplsVpnName' in module MPLS-VPN-MIB (/usr/local/share/snmp/mibs/mib-jnx-ldp.txt)
IF-MIB::ifDescr.2500 = STRING: ae0.784
Timeout: No Response from 182.19.96.13

Not all those lines which you get in the output are seen by the grep command. The 1st and 3rd lines have been written by your command snmpwalk to standard error (by default the terminal). The 2nd line has been filtered by the grep command.

Is this what you need?
Code:
snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv 182.19.96.13 2>/dev/null|awk '/ae0\.784/{print $NF}'

# 3  
Old 10-10-2012
If you want a command to terminate after some output has been detected ...

If the command will continue to generate output after the match is found, and if the wait time (could be a fraction of a second, could be weeks) until the command next attempts to write data to a pipe is acceptable, then a simple filter (awk, sed, etc...), which exits immediately after finding the match, could be sufficient. When the command next attempts to write to the pipe, since the filter that was on the other end is now gone, it will be sent a SIGPIPE (default response to this signal is program termination).

If the command to be killed does not write to the pipe regularly, then you need to send it a signal yourself when the match is detected. For this, you need to know the PID of that command.

Regards,
Alister
# 4  
Old 10-10-2012
thanks all for the reply..

Hi team,

thanks elixir_sinari

the below command given by elixir works for me but in my output I want
some other column to be displayed(it is displaying the same output as i m searching for) and also it should write it to some other file, for e.g. output will be like..
IF-MIB::ifDescr.2500 = STRING: ae0.784

then I want highlighted part to be displayed in output..


snmpwalk -v 3 -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv 1.1.1.1 2>/dev/null|awk '/ae0\.784/{print $NF}'

Kindly help on this.

thanks..
# 5  
Old 10-10-2012
Not every awk out there allows for this, but you can try to set the awk field separator to "[: ]" and then print $3.

Last edited by RudiC; 10-11-2012 at 04:57 AM.. Reason: typo
# 6  
Old 10-14-2012
Hi RudiC,

thanks for the reply...could you please write same command and put awk whereever require..I am not getting where to use awk as suggested by you...

thanks in advance!!!

Regards,
Hanumant
# 7  
Old 10-14-2012
As elixir_sinari and alister pointed out, there are some improvements possible to your command line to get where you want.
  1. Two of the three lines you show in post #1 are error msgs output to stderr. No piped filter will see those unless you redirect stderr.
  2. The grep cmd is pointless. awk can do this.
  3. If you want do stop after the first matched output, you could close the pipe.
Given your post #1 sample, my comments to elixir_sinari's suggestion combined with alister's might do the job:
Code:
snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv 182.19.96.13 2>/dev/null|awk -F"[: ]" '/ae0\.784/{print $3; exit}' > outputfile

It will suppress error msgs, print the ifDescr.2500 to the outputfile as desired, and then exit and close the pipe, (hopefully) causing the snmpwalk cmd to terminate. Pls check out and come back with results.

Last edited by RudiC; 10-14-2012 at 02:35 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

2. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Linux command to output from a string

Hi All, I have a file with name Is there a LINUX command that will help me to output the word after the 9th Underscore(_). ie the output should be DLY in this case. Can anybody pls help me. Thanks much in advance, Freddie (4 Replies)
Discussion started by: dsfreddie
4 Replies

4. Shell Programming and Scripting

When i am trying to execute export command within a shell script it is saying command not found.

I am running the export command within a view to use that value inside my build script. But while executing it it is saying "export command not found" My code is as follows: -------------------------- #!/bin/sh user="test" DIR="/bldtmp/"$user VIEW="test.view1" echo "TMPDIR before export... (4 Replies)
Discussion started by: dchoudhury
4 Replies

5. Shell Programming and Scripting

Command output string manipulation possible in one line?

This has been bothering me for 3 days. $> hostname cepsun64amd And I just want "cepsun", I would normally do h=`hostname`; ${h%%64*} But I am looking for a one-liner just for my own knowledge, because if there is a way to do this, I should know it by now. Anyway, so is this... (2 Replies)
Discussion started by: Ryan.
2 Replies

6. Shell Programming and Scripting

strange behaviour script terminate before complete execution

I'm working on a script to make backup of various folder located on various host using different OS. I got a strange behaviour because the script donět process all lines of a configuration file, the script execute only one loop even the input file have 6 lines: This is the script: #!/bin/bash... (4 Replies)
Discussion started by: |UVI|
4 Replies

7. Shell Programming and Scripting

Terminate initially if error found...

Hi, I have written a shell script which is a combination of 5 scripts into one. We have a Record Claim indicator in the scpt ($rc) with which we can come to an conclusion if the script failed to load the data or if the data loaded successfully. Can any one please help me as to how to... (16 Replies)
Discussion started by: msrahman
16 Replies

8. Shell Programming and Scripting

grep only a string on command output

How can I grep exactly a string that has .,/ characters using grep? Example: I want to grep ONLY string1 and not string1.more or string1.more.evenmore #lsauth ALL|grep 'string1' All output: string1 <--- This is the only I want. string1.more string1.evenmore. more.string1... (4 Replies)
Discussion started by: iga3725
4 Replies

9. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

10. Programming

command to terminate

hi all how to terminate command eecution process. can you please show me the way thank you (2 Replies)
Discussion started by: munna_dude
2 Replies
Login or Register to Ask a Question