Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-26-2012, 05:57 PM   #1
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?

Old 11-26-2012, 07:03 PM   #2
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,978
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"...


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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 10: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 01:05 AM
Creating array from command output (bash shell script) Rizla Programming 5 01-26-2011 11:47 PM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 05:44 AM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-03-2009 12:18 AM > Forums > Non-*NIX Forums > Programming

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

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