Skip to content
Advertisement

Bash Array not accepting WildCard

I have an Array that I have setup in a bash script. My goal is to ping through a particular port on a a server with many network interfaces. For example the ping -I eth3 172.26.0.1 command to force ping through eth3

When I setup a bash Array I can get code to work if I call the Elements (ports) individually. For example here I tell it to ping Element 2 or eth5

ethernet[0]='eth3'
ethernet[1]='eth4'
ethernet[2]='eth5'
ethernet[3]='eth6'

ping -c 1 -I ${ethernet[2]} 172.26.0.1

The script works and pings through eth2

[13:49:35] shock:/dumps # bash -x ARRAY
+ ethernet[0]=eth3
+ ethernet[1]=eth4
+ ethernet[2]=eth5
+ ethernet[3]=eth6
+ ping -c 1 -I eth5 172.26.0.1
PING 172.26.0.1 (172.26.0.1) from 172.26.0.192 eth5: 56(84) bytes of data.
From 172.26.0.192 icmp_seq=1 Destination Host Unreachable

--- 172.26.0.1 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 3001ms

However if I use a wildcard instead of just element 2 it dies on the second element (Eth4)

ethernet[0]='eth3'
ethernet[1]='eth4'
ethernet[2]='eth5'
ethernet[3]='eth6'


ping -c 1 -I ${ethernet[*]} 172.26.0.1

[13:48:12] shock:/dumps # bash -x ARRAY
+ ethernet[0]=eth3
+ ethernet[1]=eth4
+ ethernet[2]=eth5
+ ethernet[3]=eth6
+ ping -c 1 -I eth3 eth4 eth5 eth6 172.26.0.1
ping: unknown host eth4

Any thoughts, as to why the wildcard is dying on the second element in the Array? I am new to scripting, I am really just trying to use what I have learned from this article and apply it to a useful networking script. Thanks

http://www.thegeekstuff.com/2010/06/bash-array-tutorial/

EDIT – I am not sure why I got voted down on this this question. Please Advise

Advertisement

Answer

The -I option only takes one interface; you need to loop over the array:

for ifc in "${ethernet[@]}"; do
    ping -c 1 -I "$ifc" 172.26.0.1
done
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement