LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2012, 04:57 PM   #1
jtn703
LQ Newbie
 
Registered: Nov 2012
Posts: 3

Rep: Reputation: Disabled
Bash: add value to array if command fails


I'm trying to loop through a list of hosts from a DNS server to get their ifspeed using ssh and running the ethtool command. The problem is that some of the servers do not have ethtool installed.

For the servers that do have ethtool installed, the value is added to the array. What I'd like to do is add an arbitrary value to the array even if ethtool fails on the sever such that a spreadsheet can be produced with the proper count of hosts, and results returned for ethtool.

I originally started with the following which gives me the speed for any host that has ethtool installed.
ifspeed_array+=($(/usr/bin/ssh $USER@$HOST sudo ethtool eth0 | grep -i "speed" | awk '{print $2}'))




I thought doing an 'or' in the grep would add a value for servers without ethtool installed but no luck there.
ifspeed_array+=($(/usr/bin/ssh $USER@$HOST sudo ethtool eth0 | grep -i "speed\|found" | awk '{print $2}'))


Any thoughts, other than installing ethtool on the servers that don't have it installed already?

Thanks!
 
Old 11-26-2012, 06:03 PM   #2
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Not sure if this will help; but you can set a default value on a variable if the value is "null"...

example

Code:
var=$(which ethtool)
echo ${var:=not_found}
--C
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to assign output from bash command ps to array ? masuch Linux - Newbie 3 10-06-2012 09:35 AM
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 12:05 AM
Creating array from command output (bash shell script) Rizla Programming 5 01-26-2011 10:47 PM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration