How to make a statement that checks if something is divisible by something else without a remainder (BASH)

22

3

#!/bin/bash


Echo “Enter a number”

Read  $number

If [$number ] ; then 

Echo “Your number is divisible by 5”

Else

Echo “Your number is not divisible by 5”

fi

the if [$number] statement is what I don't know how to set up

Roger

Posted 2009-10-01T23:20:14.047

Reputation:

Welcome, Roger. Can you please wrap the code in your question in code tags (or use the code button on the editor)? It makes things a lot easier to read. – Telemachus – 2009-10-01T23:34:08.760

Answers

40

You can use a simpler syntax in Bash than some of the others shown here:

#!/bin/bash
read -p "Enter a number " number    # read can output the prompt for you.
if (( $number % 5 == 0 ))           # no need for brackets
then
    echo "Your number is divisible by 5"
else
    echo "Your number is not divisible by 5"
fi

Paused until further notice.

Posted 2009-10-01T23:20:14.047

Reputation: 86 075

This will result in the error message, "((: 08: value too great for base (error token is "08")" and "((: 09: value too great for base (error token is "09")" see http://ubuntuforums.org/showthread.php?t=677751 for explanation.

– Red Cricket – 2015-08-06T16:03:18.290

@RedCricket: Only if you enter a leading zero. If that's an issue, you can do: if (( 10#$number % 5 == 0 )) to force $number to be interpreted as base 10 (instead of base 8/octal implied by the leading zero). – Paused until further notice. – 2015-08-06T16:39:40.123

@Deniis Williamson Yes that is more accurate. Thanks! :) – Red Cricket – 2015-08-06T17:15:10.487

thanks! i knew there had to be a simpler way but wasn't having any luck. bash scripting has always been a bit of a black art to me. – quack quixote – 2009-10-04T10:06:25.433

11

No bc needed as long as it's integer math (you'll need bc for floating point though): In bash, the (( )) operator works like expr.

As others have pointed out the operation you want is modulo (%).

#!/bin/bash  

echo "Enter a number"
read number

if [ $(( $number % 5 )) -eq 0 ] ; then
   echo "Your number is divisible by 5"
else
   echo "Your number is not divisible by 5"
fi

quack quixote

Posted 2009-10-01T23:20:14.047

Reputation: 37 382

3

How about using the bc command:

!/usr/bin/bash

echo “Enter a number”
read number
echo “Enter divisor”
read divisor
remainder=`echo "${number}%${divisor}" | bc`
echo "Remainder: $remainder"

if [ "$remainder" == "0" ] ; then
        echo “Your number is divisible by $divisor”
else
        echo “Your number is not divisible by $divisor”
fi

user4358

Posted 2009-10-01T23:20:14.047

Reputation:

1Alternatively, you could use expr instead of bc: remainder=expr $number % $divisor – Dan Dyer – 2009-10-01T23:35:05.327

@Dan Yes it should suffice for the OP. However, I think since bc specialises in calculations, it can handle stuff like 33.3 % 11.1 which expr would likely choke on. – None – 2009-10-01T23:48:56.093

would definitely choke; expr and (( )) only handle integer math. – quack quixote – 2009-10-02T00:03:45.037

3

Nagul's answer is great, but just fyi, the operation you want is modulus (or modulo) and the operator is generally %.

Telemachus

Posted 2009-10-01T23:20:14.047

Reputation: 5 695

1

I have done it in a different way. Check if it works for you.
Example 1 :

num=11;
[ `echo $num/3*3 | bc` -eq $num ] && echo "is divisible" || echo "not divisible"
Output : not divisible

Example 2 :

num=12;
[ `echo $num/3*3 | bc` -eq $num ] && echo "is divisible" || echo "not divisible"
Output : is divisible

Simple logic.

12 / 3 = 4
4 * 3 = 12 --> same number

11 / 3 = 3
3 * 3 = 9 --> not same number

smilyface

Posted 2009-10-01T23:20:14.047

Reputation: 111

0

Just in the interest of syntax neutrality and mending the overt infix notation bias around these parts, I've modified nagul's solution to use dc.

!/usr/bin/bash

echo “Enter a number”
read number
echo “Enter divisor”
read divisor
remainder=$(echo "${number} ${divisor}%p" | dc)
echo "Remainder: $remainder"

if [ "$remainder" == "0" ]
then
        echo “Your number is divisible by $divisor”
else
        echo “Your number is not divisible by $divisor”
fi

Eroen

Posted 2009-10-01T23:20:14.047

Reputation: 5 615

1@AreusAstarte: It means you don't have dc installed. – Paused until further notice. – 2014-07-30T19:54:39.520

I realize that this is a really old question but I have a question about the code. I'm relatively new to bash and just tried to run this script. However it gives me a few errors and I honestly don't know why. After entering the number and divisor I get:

test.sh: 7: test.sh: dc: not found

Remainder:

test.sh: 10: [: unexpected operator

“Your number is not divisible by 2”

Do you have any idea why? – AreusAstarte – 2014-01-02T11:11:57.803

0

You could also use expr like so:

#!/bin/sh

echo -n "Enter a number: "
read number
if [ `expr $number % 5` -eq 0 ]
then
    echo "Your number is divisible by 5"
else
    echo "Your number is not divisible by 5"
fi

Red Cricket

Posted 2009-10-01T23:20:14.047

Reputation: 111