我有一个数组,我已在bash脚本中设置。我的目标是通过具有多个网络接口的服务器上的特定端口进行ping。例如ping -I eth3 172.26.0.1命令强制通过eth3 ping命令Bash数组不接受通配符
当我设置一个bash数组时,如果我单独调用元素(端口),我可以使代码工作。比如在这里我告诉它平元2或eth5
ethernet[0]='eth3'
ethernet[1]='eth4'
ethernet[2]='eth5'
ethernet[3]='eth6'
ping -c 1 -I ${ethernet[2]} 172.26.0.1
该脚本和坪通过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
但是如果我使用通配符而不只是元件2它死的第二个元素上(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
任何想法,至于为什么通配符在阵列中的第二个元素上死亡?我不熟悉脚本编写,我只是尝试使用从本文中学到的知识并将其应用于有用的网络脚本。由于
http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
编辑 - 我不知道为什么我被否决了这个这个问题。请指教
正在创建是错误的ping命令。您需要为每个接口发出单独的ping命令。 – stark
当您看到'eth3 eth4 eth5 eth6'时''{ethernet [*]}'扩展到所有数组元素。在$ {ethernet [*]}中使用'for'循环来解决这个问题。做ping -c 1 -I $ i 172.26.0.1;完成' –
@NarūnasK引用可变扩展。特别是使用'[@]'进行数组扩展以保证它们对于具有空格的值是安全的。 –