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.

 $ 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
Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply