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
# 8  
Old 10-18-2012
Hi All,

the command RudC is working fine alone..but I need this to check for multiple addresses and here my script to get ifdescr for multiple addresses, it is not giving me proper output(no output) can anyonce suggest what is wrong here..
Code:
#!/bin/bash
{
if [ -f ifdesc.out ];
then
`rm ifdesc.out`
fi
exec<ifdesc.csv
while IFS=',' read -r f1 f2
do
snmpwalk -v 3  -u snmp_com -a MD5 -A vfies -x DES -X VfIeis -l authPriv $f1 2>/dev/null|awk -F"[: ]" '/$f2/{print $3; exit}' > ifdesc.out
done
} > /dev/null 2>&1

where $f1=ipaddres and $f2 is the interface name.

Please help out with this..

Thanks,
Hanumant

Last edited by Franklin52; 10-18-2012 at 08:19 AM.. Reason: Please use code tags for data and code samples
# 9  
Old 10-18-2012
pls. use code tags!
You can't have shell variables expanded within single quotes. The correct way to get f1 into the awk command is to add an awk variable assignment before or after the awk expression and use this inside the command:
Code:
awk -F"[: ]" '/F2awk/{print $3; exit}' F2awk=$f2

# 10  
Old 10-18-2012
Quote:
Originally Posted by RudiC
pls. use code tags!
You can't have shell variables expanded within single quotes. The correct way to get f1 into the awk command is to add an awk variable assignment before or after the awk expression and use this inside the command:
Code:
awk -F"[: ]" '/F2awk/{print $3; exit}' F2awk=$f2

That's incorrect usage of an awk variable in a regexp match. That'll be treated as a match for the pattern "F2awk" and not that for the value contained in the variable by the same name. And, use double quotes to prevent the shell from messing with the data with whitespaces.

The correct usage is:
Code:
awk -F"[: ]" '$0 ~ F2awk{print $3; exit}' F2awk="$f2"


Last edited by elixir_sinari; 10-18-2012 at 08:02 AM..
# 11  
Old 10-18-2012
Hi,

thanks to both of you for reply..

here is the updated script...still not working..when i run it it keeps on running but don't stop..and also no output in output file,
Code:
#!/bin/bash
{
if [ -f ifdesc.out ];
then
`rm ifdesc.out`
fi
exec<ifdesc.csv
while IFS=',' read -r f1 f2
do
snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv $f1 2>/dev/null|awk -F"[: ]" '$0 ~ F2awk{print $3; exit}' F2awk="$f2" > ifdesc.out
done
}


we are near to close this ..plz help..

---------- Post updated at 07:15 AM ---------- Previous update was at 07:09 AM ----------

hey ...here I got the output in my output file..but i feed 5 IP and I got output of only one IP i.e. last IP in the list..plz help me...it should print new output for new ip address on new line..and this will be done...

thanks ...

Last edited by Franklin52; 10-18-2012 at 10:31 AM.. Reason: Please use code tags for data and code samples
# 12  
Old 10-18-2012
Use >> .
> deletes old data and writes new data to the file..Smilie

Code:
snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l  authPriv $f1 2>/dev/null|awk -F"[: ]" '$0 ~ F2awk{print $3; exit}'  F2awk="$f2" >> ifdesc.out

# 13  
Old 10-18-2012
Quote:
Originally Posted by elixir_sinari
That's incorrect usage of an awk variable in a regexp match. ...
You're right. I did it on the fly and had no chance to test. Sorry for the premature post.
# 14  
Old 10-19-2012
Hiii...yupeeii...I am getting the required output in my output file now....thaks to all those who helped me to acheive this..but still need to make it more finetune..

here my script...

Code:
#!/bin/bash
{
if [ -f ifdesc.out ];
then
`rm ifdesc.out`
fi
exec<ifdesc.csv
while IFS=',' read -r f1 f2
do
snmpwalk -v 3  -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv $f1 2>/dev/null|awk -F"[: ]" '$0 ~ F2awk{print $3; exit}' F2awk="$f2" >> ifdesc.out
done
}

In this script..it may be possible that few devices may never give out any result i.e. snmpwalk output in this case it will directly print the next result in series..what I need is put the IP Address in one column and its respective result (interface ifindex) in next column so that it will be very easy to identify which IP have not responded to SNMP query.

Thanks again to ALL experts!!!

---------- Post updated 10-19-12 at 12:45 AM ---------- Previous update was 10-18-12 at 08:41 AM ----------

Hi All,

please reply to my bove query.

regards,
hanumant

Last edited by Scott; 10-18-2012 at 10:45 AM.. Reason: Code tags, PLEASE...
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