Jump to content

Bash scripting - Sum of values = odd but echos even?

For some reason even if the sum of the two numbers is odd it echos thats it's even. I don't understand why. Can anyone idenify the problem?

 

Here is the code and a screenshot of the current output:

#!/bin/bashecho Enter value one:read valueOneecho Enter value two:read valueTwototal=$((valueOne+valueTwo))if ! [[ $valueOne =~ ^[0-9]+$ ]];    then        echo $valueOne "is not a number"fiif ! [[ $valueTwo =~ ^[0-9]+$ ]];    then        echo $valueTwo "is not a number"fiif [[ $total = %2!=0 ]]; then   echo $total "is an odd number"else   echo $total "is an even number" fi   

post-225550-0-12293900-1448482976_thumb.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

For starters, there's a syntax error in your 'if' clause:

if [[ $total = %2!=0 ]]; then
Check how the modulo operator works, you should be able to spot the error fairly quickly

once you've looked that up (for example here: http://www.tldp.org/LDP/abs/html/ops.html )

The second thing you want to look for is bash and arithmetic evaluation of expressions,

as discussed here: http://stackoverflow.com/questions/2552130/bash-evaluate-a-mathematical-term

Once you've looked those two things up, you should be able to get this working I think.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites


#!/bin/bash

echo Enter value one:

read valueOne

echo Enter value two:

read valueTwo

total=$((valueOne+valueTwo))

mod=$(($total % 2))

if ! [[ $valueOne =~ ^[0-9]+$ ]];

then

echo $valueOne "is not a number"

fi

if ! [[ $valueTwo =~ ^[0-9]+$ ]];

then

echo $valueTwo "is not a number"

fi

if [ $mod != 0 ]; then

echo $total "is an odd number"

else

echo $total "is an even number"

fi

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
Share on other sites

Link to post
Share on other sites

#!/bin/bashecho Enter value one:read valueOneecho Enter value two:read valueTwototal=$((valueOne+valueTwo))mod=$(($total % 2))if ! [[ $valueOne =~ ^[0-9]+$ ]];    then        echo $valueOne "is not a number"fiif ! [[ $valueTwo =~ ^[0-9]+$ ]];    then        echo $valueTwo "is not a number"fiif [ $mod != 0 ]; then   echo $total "is an odd number"else   echo $total "is an even number"fi

Hmm I've just realised that i only want the odd even to work if it's a number only. I can't figure that out either. If a letter is entered the odd even shouldn't echo.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

 

#!/bin/bash

echo Enter value one:
read valueOne
echo Enter value two:
read valueTwo
 
 
if ! [[ $valueOne =~ ^[0-9]+$ ]];
    then
        echo $valueOne "is not a number"
        exit
fi
 
if ! [[ $valueTwo =~ ^[0-9]+$ ]];
    then
        echo $valueTwo "is not a number"
        exit
fi
echo -e "\n"
total=$((valueOne+valueTwo))
mod=$(($total % 2))
 
 
if [ $mod != 0 ]; then
   echo $total "is an odd number"
else
   echo $total "is an even number"
fi
 
Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×