Using boolean expression Bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using boolean expression Bash script
# 1  
Old 02-09-2016
Using boolean expression Bash script

Hi,
Im 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:
Code:
#!/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?

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data, thanks

Last edited by Miron; 02-09-2016 at 10:43 AM.. Reason: code tags
# 2  
Old 02-09-2016
Can't you apply/modify the priciples of this post?
# 3  
Old 02-10-2016
Im new begining in Bash script, so your script is hard for me to understand.
couldnt you help me with my simple script Smilie
# 4  
Old 02-10-2016
I'd love to - if I only understood what you're after.
The mentioned scriptlet creates a 32-bit binary equivalent of an IP4 address and then XORs it with another. So - analyzing and testing it line by line could help you do what you need.
# 5  
Old 02-10-2016
First i converted both deimal ip to binary.
i need to make a for loop that will compare the binary bits (bit by bit) (using XNOR) of first and second ip

XNOR theory:
1- if the bits of first and second ip equal, result will be 1
2- if they are diferent the result will be 0

But if the result become 0 of any two bits, the comparisation will stop and put 0 to the another bits without compare them
# 6  
Old 02-10-2016
How about this:

Code:
#!/bin/bash
dec2bin () {
  # dec2bin num [width] [var]
  local bin=""
  for((num=$1;num;num/=2))
  do
    bin=$((num%2))$bin
  done
  ((${#3})) &&
      printf -v ${3} "%0*d" ${2:-1} $bin ||
      printf "%0*d" ${2:-1} $bin
}

xnor () {
    # xnor bits1 bits2 [var]
    local res=""
    for((bit=0;bit<${#1};bit++))
    do
        if ((!done&&${1:bit:1}==${2:bit:1}))
        then
          res=${res}1
        else
          done=1
          res=${res}0
        fi
    done
    ((${#3})) &&
      printf -v ${3} "%d" $((2#$res)) ||
      printf "%d" $((2#$res))
}

echo "Enter the first ip"
read ip1
echo "Enter the second ip"
read ip2

#Separate octs of first ip
set ${ip1//./ }

#convert decimal to binary
dec2bin $1 8 a1
dec2bin $2 8 a2
dec2bin $3 8 a3
dec2bin $4 8 a4

#Separate octs of second ip
set ${ip2//./ }

#convert decimal to binary (second IP)
dec2bin $1 8 b1
dec2bin $2 8 b2
dec2bin $3 8 b3
dec2bin $4 8 b4

done=0
xnor $a1 $b1 c1
xnor $a2 $b2 c2
xnor $a3 $b3 c3
xnor $a4 $b4 c4

echo $c1.$c2.$c3.$c4



Result:

Code:
Enter the first ip
192.168.16.55
Enter the second ip
192.168.7.55
255.255.224.0

# 7  
Old 02-12-2016
Thanks Chubler_XL for ur answer
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a Boolean variable which succeed and failed inside the if loop in shell script ?

I have if loop with multiple variable value check in if loop. How can i print only if loop satisfied variable and its value in shell script ? I dont want to check each variable in if loop. That makes my script larger. if ] then echo "Only satisfied variable with value" ... (3 Replies)
Discussion started by: prince1987
3 Replies

2. Shell Programming and Scripting

How to assign result of boolean expression?

Hello I would to write the test on one line like : declare -i x=0 y=0 ........ some code assign value 0 or 1 to x and y ........ # if either x or y or both is set to 1, then do something if -o ; then do_something fi Any help is welcome (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Getting error in bash script; expr $a + 1: integer expression expected

Hi, I am new to shell/bash script. I am trying to run below script #!/bin/bash a=0 b=10 if then echo "a is equal to be" else echo "a is not equal to be" fi MAX=10 while do echo $a a='expr $a + 1' done (1 Reply)
Discussion started by: Mallikgm
1 Replies

4. Shell Programming and Scripting

Boolean expression

hi, im learning python language. and my teacher gives me this question on class: Boolean expression : not (p or not q) what is the correct answer for that? i still dont understand, and please give me a link for a new beginner in python to learn. thanks (1 Reply)
Discussion started by: jazzyzha
1 Replies

5. Shell Programming and Scripting

Hi im new to bash scripting I want to know what does the regex expression do ??

# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi (1 Reply)
Discussion started by: kevin298
1 Replies

6. UNIX for Dummies Questions & Answers

Im new to bash scriping and i found this expression on a bash script what does this mean.

# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi espeacailly the top regex part? ---------- Post updated at 06:58 PM ---------- Previous update was... (1 Reply)
Discussion started by: kevin298
1 Replies

7. Shell Programming and Scripting

boolean expression in bash

Can someone, please, help me to make this condition valid/accepted in bash? I really cannot. I'm stuck with the brackets... This one tells me: missing `]' if ]; then # NOTIFY ERROR... fi And... I'd also appreciate a link to bash documents that explain these things. All... (2 Replies)
Discussion started by: mamboknave
2 Replies

8. Shell Programming and Scripting

Boolean expression issues

Hi everybody: I'm working on a script to send emails with logs attached based on one single rule..."check if the number of errors has increased since the last time the script ran" Basically what my script does is read from a previous file with the last trace of errors the previous error... (3 Replies)
Discussion started by: hyunkel_01
3 Replies

9. Shell Programming and Scripting

[Bash]Function returning a boolean

Hello all, I would like to know if it is possible to return a the result of a boolean expression from a function like this function() { # some code return || } and what will be the return value ? Thank you for help. (6 Replies)
Discussion started by: dolphin06
6 Replies

10. Shell Programming and Scripting

Regular expression boolean in perl

How can I find out, that whether a regular expression are matched or not(as a boolean)?! tnx in advance. (2 Replies)
Discussion started by: Zaxon
2 Replies
Login or Register to Ask a Question