Shell script for using modulo operator, finding if number is dividable and print remainder.

Following shell script helps to understand how to use modulo operator in bash and identify if number is completely dividable by another number and print the remainder.
This script accepts two arguments on the commands line, first number is Divident and second number is Divisor.

 $ vim understanding_modulo_operator.sh 
#!/bin/bash
NUMBER=$1
DIVIDE_BY=$2
remainder=$((NUMBER%DIVIDE_BY))
echo "Module operation of $NUMBER%$DIVIDE_BY is $remainder"

if [ $remainder == "0" ]
then
    echo "$NUMBER: is completely dividable by $DIVIDE_BY"
else
    echo "$NUMBER: is not dividable by $DIVIDE_BY"
fi

Now, lets assume we want to divide 10 by 2 first and 10 by 3 next.


Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )

   


$ bash understanding_modulo_operator.sh 10 2
Module operation of 10%2 is 0
10: is completely dividable by 2
$ bash understanding_modulo_operator.sh 10 3 
Module operation of 10%3 is 1
10: is not dividable by 3
lynxbee_ezoic

Leave a Comment

Android Android Build system Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded