我试图在if/else语句中省略广播ip地址(192.168.0.255)。
我想省略(newo)变量中的广播ip。
当我还在学习的时候,请对我保持冷静。如果有其他方法压缩这个工作流,请告诉我。谢谢。:)
bcast="$(ifconfig | grep broadcast | awk '{print $6}')"
oldo="$(arp -a)"
ping -c 1 "$bcast"
sleep 3
killall ping
newo="$(arp -a)"
if [ "$oldo" = "$newo" ]
then
echo="match"
else
echo="no match"
fi边注:当我ping广播时,这就是为什么它显示在(newo)变量中的原因。这将是我在比较这两个字符串时忽略该字符串的原因。
结果:,为了隐私,我省略了所有的MAC地址。
++ ifconfig
++ grep broadcast
++ awk '{print $6}'
+ bcast=192.168.0.255
++ arp -a
+ oldo='? (192.168.0.1) at on en1 ifscope [ethernet]
? (192.168.0.11) at on en1 ifscope [ethernet]
? (192.168.0.14) at on en1 ifscope [ethernet]
? (192.168.0.15) at on en1 ifscope [ethernet]
? (192.168.0.17) at on en1 ifscope [ethernet]
? (192.168.0.19) at on en1 ifscope [ethernet]
? (192.168.0.20) at on en1 ifscope [ethernet]
? (192.168.0.22) at on en1 ifscope permanent [ethernet]
? (192.168.0.25) at on en1 ifscope [ethernet]
? (192.168.0.255) at ff:ff:ff:ff:ff:ff on en1 ifscope [ethernet]
? (224.0.0.251) at on en1 ifscope permanent [ethernet]
? (239.255.255.250) at on en1 ifscope permanent [ethernet]'
+ ping -c 1 192.168.0.255
PING 192.168.0.255 (192.168.0.255): 56 data bytes
64 bytes from 192.168.0.22: icmp_seq=0 ttl=64 time=0.155 ms
--- 192.168.0.255 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.155/0.155/0.155/0.000 ms
+ sleep 3
+ killall ping
No matching processes belonging to you were found
++ arp -a
+ newo='? (192.168.0.1) at on en1 ifscope [ethernet]
? (192.168.0.11) at on en1 ifscope [ethernet]
? (192.168.0.14) at on en1 ifscope [ethernet]
? (192.168.0.15) at on en1 ifscope [ethernet]
? (192.168.0.17) at on en1 ifscope [ethernet]
? (192.168.0.19) at on en1 ifscope [ethernet]
? (192.168.0.20) at on en1 ifscope [ethernet]
? (192.168.0.22) at on en1 ifscope permanent [ethernet]
? (192.168.0.25) at on en1 ifscope [ethernet]
? (192.168.0.255) at ff:ff:ff:ff:ff:ff on en1 ifscope [ethernet]
? (224.0.0.251) at on en1 ifscope permanent [ethernet]
? (239.255.255.250) at on en1 ifscope permanent [ethernet]'
+ '[' '? (192.168.0.1) at on en1 ifscope [ethernet]
? (192.168.0.11) at on en1 ifscope [ethernet]
? (192.168.0.14) at on en1 ifscope [ethernet]
? (192.168.0.15) at on en1 ifscope [ethernet]
? (192.168.0.17) at on en1 ifscope [ethernet]
? (192.168.0.19) at on en1 ifscope [ethernet]
? (192.168.0.20) at on en1 ifscope [ethernet]
? (192.168.0.22) at on en1 ifscope permanent [ethernet]
? (192.168.0.25) at on en1 ifscope [ethernet]
? (192.168.0.255) at ff:ff:ff:ff:ff:ff on en1 ifscope [ethernet]
? (224.0.0.251) at on en1 ifscope permanent [ethernet]
? (239.255.255.250) at on en1 ifscope permanent [ethernet]' = '? (192.168.0.1) at on en1 ifscope [ethernet]
? (192.168.0.11) at on en1 ifscope [ethernet]
? (192.168.0.14) at on en1 ifscope [ethernet]
? (192.168.0.15) at on en1 ifscope [ethernet]
? (192.168.0.17) at on en1 ifscope [ethernet]
? (192.168.0.19) at on en1 ifscope [ethernet]
? (192.168.0.20) at on en1 ifscope [ethernet]
? (192.168.0.22) at on en1 ifscope permanent [ethernet]
? (192.168.0.25) at on en1 ifscope [ethernet]
? (192.168.0.255) at ff:ff:ff:ff:ff:ff on en1 ifscope [ethernet]
? (224.0.0.251) at on en1 ifscope permanent [ethernet]
? (239.255.255.250) at on en1 ifscope permanent [ethernet]' ']'
+ echo=match发布于 2016-08-11 23:10:14
如果您只是试图确保广播地址不会出现在newo中,请尝试:
bcast=$(ifconfig | awk '/broadcast/{print $6}')
newo=$(arp -a | awk '!match($2,"("b")")' b=$bcast)https://stackoverflow.com/questions/38907203
复制相似问题