Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
#!/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

share|improve this question
    
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 Oct 1 '09 at 23:34
add comment

6 Answers

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
share|improve this answer
add comment

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
share|improve this answer
    
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 Oct 4 '09 at 10:06
add comment

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
share|improve this answer
1  
Alternatively, you could use expr instead of bc: remainder=expr $number % $divisor –  Dan Dyer Oct 1 '09 at 23:35
    
@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. –  nagul Oct 1 '09 at 23:48
    
would definitely choke; expr and (( )) only handle integer math. –  quack quixote Oct 2 '09 at 0:03
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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
share|improve this answer
    
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 Jan 2 at 11:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.