Sponsored Content
Top Forums Shell Programming and Scripting Using boolean expression Bash script Post 302966195 by Miron on Tuesday 9th of February 2016 08:44:17 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
BASHBUG(1)							      bashbug								BASHBUG(1)

NAME
bashbug - report a bug in bash SYNOPSIS
bashbug [--help] [--version] [bug-report-email-addresses] DESCRIPTION
bashbug is a utility for reporting bugs in Bash to the maintainers. bashbug will start up your preferred editor with a preformatted bug report template for you to fill in. Save the file and quit the editor once you have completed the missing fields. bashbug will notify you of any problems with the report and ask for confirmation before sending it. By default the bug report is mailed to both the GNU developers and the Debian Bash maintainers. The recipients can be changed by giving a comma separated list of bug-report-email-addresses. If you invoke bashbug by accident, just quit your editor. You will always be asked for confirmation before a bug report is sent. OPTIONS
--help Show a brief usage message and exit. --version Show the version of bashbug and exit. bug-report-email-addresses Comma separated list of recipients' email addresses. By default the report is mailed to both the GNU developers and the Debian Bash maintainers. ENVIRONMENT
DEFEDITOR Editor to use for editing the bug report. EDITOR Editor to use for editing the bug report (overridden by DEFEDITOR). SEE ALSO
bash(1), reportbug(1), update-alternatives(8) for preferred editor. AUTHOR
This manual page was written by Christer Andersson <klamm@comhem.se> for the Debian project (but may be used by others). GNU Bash 3.1 11 December 2007 BASHBUG(1)
All times are GMT -4. The time now is 03:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy