I'm trying to write a Bash script that calculates the least common subnet for two address.
Theoretical: I have to change IP from decimal to binary, then apply XNOR on the two IPs.
I tried to write this:
#!/bin/bash
echo "Ebter the first ip"
read ip1
echo "Enter the second ip"
read ip2
#Separate octs of first ip
a1=`echo $ip1 | awk 'BEGIN{FS="."}{print $1}'`
a2=`echo $ip1 | awk 'BEGIN{FS="."}{print $2}'`
a3=`echo $ip1 | awk 'BEGIN{FS="."}{print $3}'`
a4=`echo $ip1 | awk 'BEGIN{FS="."}{print $4}'`
#convert decimal to binary
b1=`echo "obase=2;$a1" | bc`
b2=`echo "obase=2;$a2" | bc`
b3=`echo "obase=2;$a3" | bc`
b4=`echo "obase=2;$a4" | bc`
#Separate octs of second ip
c1=`echo $ip2 | awk 'BEGIN{FS="."}{print $1}'`
c2=`echo $ip2 | awk 'BEGIN{FS="."}{print $2}'`
c3=`echo $ip2 | awk 'BEGIN{FS="."}{print $3}'`
c4=`echo $ip2 | awk 'BEGIN{FS="."}{print $4}'`
#convert decimal to binary (second IP)
d1=`echo "obase=2;$c1" | bc`
d2=`echo "obase=2;$c2" | bc`
d3=`echo "obase=2;$c3" | bc`
d4=`echo "obase=2;$c4" | bc`
e1=`echo $b1 || $d1 | rev`
e2=`echo $b2 || $d2 | rev`
e3=`echo $b3 || $d3 | rev`
e4=`echo $b4 || $d4 | rev`
echo "$e1.$e2.$e3.$e4"
I have two problems:
I need to apply XNOR to the binary IP (bit by bit), but if the result of a specific bit becomes zero I want to stop the operation and make another bit that follows the zero, zero also.
I need that XNOR ignores ".".
Can someone help me with this issue please?