James Dean James Dean - 3 months ago 16
Bash Question

How do I omit a certain string when comparing variables?

I'm trying to omit the broadcast ip address (192.168.0.255) in the if/else statement.

I would like to omit the broadcast ip from the (newo) variable.

Please be lite on me as I'm still learning. If there's another way to compress this workflow, let me know. Thanks. :)

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


SIDE NOTE:
when I ping the broadcast, this is the reason why it shows in the (newo) variable. This would be my reason to ignore that string when comparing the two.

RESULTS: I've omitted all MAC address for privacy.

++ 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

Answer

If you are merely trying to ensure that the broadcast address does not appear in newo, try:

bcast=$(ifconfig | awk '/broadcast/{print $6}')  
newo=$(arp -a | awk '!match($2,"("b")")' b=$bcast)
Comments