Bash – Integer Arithmetic Operations

Background

Wanted to get down sample tooling for performing arithmetic options in Bash.

Code


#!/bin/bash

function compareNumbers()
{

	local _message=""

	_number1=($1)
	_number2=($2)

	if [ $_number1 -lt $_number2 ]
	then
	  _message="The first number ( $_number1 ) is less than the second number ( $_number2 ) "

	elif [ $_number1 -eq $_number2 ]

	then
	  _message="The first number ( $_number1 ) is equal to the second number ( $_number2 ) "

	elif [ $_number1 -gt $_number2 ]

	then
	  _message="The first number ( $_number1 ) is greater than the second number ( $_number2 ) "

	else

	  _message="The first number ( $_number1 ) and second number ( $_number2 ) are misunderstood!"

	fi

	echo $_message

}

function calc()
{

	local _message=""
    local _moduleName=`basename "$0"`

	_number1=($1)
    _operator=($2)

	_number2=($3)

    local _result=0
    local _operatorAsString=""

	_operatorAsString="$_operator"

    case "$_operator" in

           +)
               _result=$(expr $_number1 + $_number2);
               ;;                       

           -)
               _result=$(expr $_number1 - $_number2);

               ;;                       

		   /)
               _result=$(expr $_number1 / $_number2);
               ;;                       

		   %)
               _result=$(expr $_number1 % $_number2);
               ;;  

		   '^' | '**')
               let "_result=$_number1**$_number2"
               ;;                  

		   '*'|'mult'|'multiply'|$_moduleName)
               _result=$(expr $_number1 \* $_number2);
               _operatorAsString="mult";
               ;;     

    esac

    _message="$_number1 $_operatorAsString $_number2 = $_result"

	echo $_message

}

declare -i _number1
declare -i _number2

declare _number1
declare _number2

#---------------------------------------------------------#
#Compare                                                  #
#---------------------------------------------------------#
_number1=10
_number2=15

echo '********************************************************'

_mesg=$(compareNumbers $_number1 $_number2 )

echo $_mesg

_number1=15
_number2=15

_mesg=$(compareNumbers $_number1 $_number2)

echo $_mesg

_number1=18
_number2=15

_mesg=$(compareNumbers $_number1 $_number2)

echo $_mesg

_number1=apple
_number2=15

_mesg=$(compareNumbers $_number1 $_number2)

echo $_mesg

echo '********************************************************'
echo
echo

#---------------------------------------------------------#
#Calc                                                     #
#---------------------------------------------------------#

# calc - addition

echo '*****************************************************'

_number1=5
_number2=3
_operator='+'

_mesg=$(calc $_number1 $_operator $_number2  )

echo $_mesg

# calc - subtract
_number1=105
_number2=90
_operator='-'

_mesg=$(calc $_number1 $_operator $_number2  )

echo $_mesg

# calc - divide
_number1=90
_number2=10
_operator='/'

_mesg=$(calc $_number1 $_operator $_number2  )

echo $_mesg

# calc - modulo
_number1=42
_number2=8
_operator='%'

_mesg=$(calc $_number1 $_operator $_number2  )

echo $_mesg

# calc - multiply
_number1=7
_number2=8
_operator='*'

_mesg=$(calc $_number1 $_operator $_number2  )

echo $_mesg

# calc - exponent
_number1=2
_number2=3
_operator='^'

_mesg=$(calc $_number1 $_operator $_number2  )

echo $_mesg

echo '********************************************************'

#---------------------------------------------------------

Takeaway

  1. Arithmetic
    • Multiplication
      • * ( Symbol)
        • Passing the multiple symbol (*) is not so easy
        • I think the reason is that it is considered a special character

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s