[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

bash script to run two commands and return single value

Posted on 2010-09-14
8
Medium Priority
?
1,741 Views
Last Modified: 2013-12-21
Hey all, Im not good at scripting but im trying to run two snmpget commands (lets say snmp1 and snmp2) aginst a server with a bash script. Then I want to take snmp1 and subtract it from snmp2 and display the results. Im running into an issue with this.

The snmpget commands return a string that contains the OID path and then the value at the end. Im trying to parse out only the value to a variable. The parsing works fine but i can't figure out how to get the values assigned to the variables. Im using awk (also tried cut) to parse and it always prints instead of assigning to the variable.

Im sure im missing something small but Im totally lost.

Could someone help me out with my code or suggest a better way to do this?

Thanks


#!/bin/bash


totalokconnects='snmpwalk -v 1 -c public 10.0.0.19 .1.3.6.1.4.1.1971.6.2.9.126.6.0'
totalstreamattempts='snmpwalk -v 1 -c public 10.0.0.19 .1.3.6.1.4.1.1971.6.2.9.126.7.0'

good=$totalokconnects  | awk '{print $4}'
total=$totalstreamattempts | awk '{print $4}'

totalerrors=$(($total-$good))

Open in new window

0
Comment
Question by:that1guy15
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 33678128
good=`$totalokconnects  | awk '{print $4}'`
0
 
LVL 48

Expert Comment

by:Tintin
ID: 33678634

good=$(echo $totalokconnects  | awk '{print $4}')
total=$(echo $totalstreamattempts | awk '{print $4}')

Open in new window

0
 
LVL 18

Expert Comment

by:TobiasHolm
ID: 33680301
Hi!

You have wrong type of "fnutts" on line 4 and 5 (you need to execute those lines as commands).
And ozo forgot the echo command.

A working script:
#!/bin/bash


totalokconnects=`snmpwalk -v 1 -c public 10.0.0.19 .1.3.6.1.4.1.1971.6.2.9.126.6.0`
totalstreamattempts=`snmpwalk -v 1 -c public 10.0.0.19 .1.3.6.1.4.1.1971.6.2.9.126.7.0`

good=`echo $totalokconnects  | awk '{print $4}'`
total=`echo $totalstreamattempts | awk '{print $4}'`

totalerrors=$(($total-$good))

Open in new window

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 84

Expert Comment

by:ozo
ID: 33680323
I did not forget the echo command,
I based it on
totalokconnects='snmpwalk -v 1 -c public 10.0.0.19 .1.3.6.1.4.1.1971.6.2.9.126.6.0'
not on
totalokconnects=`snmpwalk -v 1 -c public 10.0.0.19 .1.3.6.1.4.1.1971.6.2.9.126.6.0`
0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 33680338
although an easier way might be
totalokconnects=(`snmpwalk -v 1 -c public 10.0.0.19 .1.3.6.1.4.1.1971.6.2.9.126.6.0`)
good=${totalokconnects[3]}
0
 
LVL 18

Expert Comment

by:TobiasHolm
ID: 33680348
>I based it on totalokconnects='snmp...

aah! I see! Then that version also works!! Multiple ways of the same result!! :)

ozo's version:
#!/bin/bash


totalokconnects='snmpwalk -v 1 -c public 10.0.0.19 .1.3.6.1.4.1.1971.6.2.9.126.6.0'
totalstreamattempts='snmpwalk -v 1 -c public 10.0.0.19 .1.3.6.1.4.1.1971.6.2.9.126.7.0'

good=`$totalokconnects  | awk '{print $4}'`
total=`$totalstreamattempts | awk '{print $4}'`

totalerrors=$(($total-$good))

Open in new window

0
 
LVL 18

Expert Comment

by:TobiasHolm
ID: 33680372
>although an easier way might be...

Yes, and create an string array instead. Much more elegant solution ozo!
0
 
LVL 23

Author Comment

by:that1guy15
ID: 33686683
Sorry for taking so long to get back. I have been working on other projects.

The first two recommendations didnt work but ozo's second recommendation did

>although an easier way might be
totalokconnects=(`snmpwalk -v 1 -c public 10.0.0.19 .1.3.6.1.4.1.1971.6.2.9.126.6.0`)
good=${totalokconnects[3]}

Took me a little bit to realize you were using ` instead of '. I thought i had good eyes but i guess not :)

Thanks for the help!
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order for businesses to be compliant with certain information security laws in some countries, you need to be able to prove that a user (which user it was becomes important to the business to take action against the user after an event has occurr…
In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question