【发布时间】:2016-12-18 19:59:03
【问题描述】:
我试图在 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
【问题讨论】:
-
文本的样例会有所帮助。你可能会使用 sed 来删除你不想要的东西,例如
newo=$(echo $newo | sed -e 's/192.168.0.255//') -
约翰,向您展示示例输出的最简单方法是什么?
-
编辑问题
oldo=blah blah blah", newo="werj werj werj"
标签: bash shell variables if-statement scripting