首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在比较变量时省略某个字符串?

如何在比较变量时省略某个字符串?
EN

Stack Overflow用户
提问于 2016-08-11 22:43:24
回答 1查看 52关注 0票数 0

我试图在if/else语句中省略广播ip地址(192.168.0.255)。

我想省略(newo)变量中的广播ip。

当我还在学习的时候,请对我保持冷静。如果有其他方法压缩这个工作流,请告诉我。谢谢。:)

代码语言:javascript
复制
 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地址。

代码语言:javascript
复制
++ 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-11 23:10:14

如果您只是试图确保广播地址不会出现在newo中,请尝试:

代码语言:javascript
复制
bcast=$(ifconfig | awk '/broadcast/{print $6}')  
newo=$(arp -a | awk '!match($2,"("b")")' b=$bcast)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38907203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档