Skip to main content

Basic Shell script to perform Mathematical functions

 #!/bin/bash

: '
 BASH Script that mimics a 4 function calculator. A function called addition. This function will 
take two arguments and will return the sum of these two arguments. Be sure to check
 that the values are valid numbers. A function called subtraction. This function will take two 
arguments and will return the difference of these two arguments. A function called multiply. 
This function will take two arguments and will return the product of these two arguments.
A function called divide. This function will take two arguments and will return the 
quotient of these two arguments. First argument is an option that chooses what to do with the
 two other arguments. The options should be as follows: -a Perform addition of the two numbers -s
 Perform subtraction of the two numbers -m Perform multiplication of the two numbers -d Perform 
division of the two numbers Second argument should be the first number used for the math problem 
Third argument should be the second number used for the math problem Examples: To add 10 + 2, run 
the script as follows: math.sh -a 10 2 To subtract 10 - 2, run the script as follows: math.sh -s 
10 2 To multiply 10 x 2, run the script as follows: math.sh -m 10 2 To divide 10 / 2, run the 
script as follows: math.sh -d 10 2 Modify your functions so it works with floating point numbers 
(can accept and return the fractional part of numbers)
'


addition(){


for i in "$@"

do 

case "${i#[-+]}" in 

*[!0-9.]* | '')

echo "Not Numbers!"

exit 2

;; 

esac

done

#echo $(($1+$2))

echo "$1 + $2" | bc



}



subtraction(){


for i in "$@"

do 

case "${i#[-+]}" in 

*[!0-9.]* | '')

echo "Not Numbers!"

exit 2

;; 

esac

done

#echo $(($1-$2))

echo "$1 - $2" | bc


}


multiply(){


for i in "$@"

do 

case "${i#[-+]}" in 

*[!0-9.]* | '')

echo "Not Numbers!"

exit 2

;; 

esac

done

#echo $(($1*$2))

echo "$1 * $2" | bc


}


divide(){


for i in "$@"

do 

case "${i#[-+]}" in 

*[!0-9.]* | '')

echo "Not Numbers!"

exit 2

;; 

esac

done

if ! [[ $2 =~ ^[1-9][0-9].*$ ]]

    then

        echo "Sorry greater then zero"

exit

fi

#echo $(($1/$2))

echo "$1 / $2" | bc


}





while getopts "a:s:m:d:" options

do

case $options in

a)

addition $2 $3

;;

s)

subtraction $2 $3


;;

m)

multiply $2 $3


;;

d)

divide $2 $3



;;

?)

echo "Invalid Flag Provided!"

exit

esac

done




Comments

Popular posts from this blog

The World of Social Networking

Encrypt/Lock Computer Drives Or Pendrive In Windows 7

                   Microsoft Bitlocker drive encryption is used to encrypt/lock any Local drive or removable drives like pendrives.Bitlocker encrypts the drives using 128 bit key.Once the drives are encrypted/locked it can only opened using the password.Microsoft introduced bitlocker encryption in windows vista.The bitlocker feature is currently available in Ultimate and Enterprise editions of windows 7 ,Vista as well as the Windows Server 2008 and Windows Server 2008 R2 .Bitlocker requires that your hard disk should have atleast two NTFS drives :One of the operating system and the other with a minimum size of 100 MB.

Download Facebook or Youtube Videos Without Any Tools

                Have you ever liked a video shared by your friend on facebook and wanted to download it…Well this this tutorial is not only for facebook or youtube instead it can be used for any video streaming website like metacafe,dailymotion,veoh or yahoo videos.There are websites on the internet that will convert your video link into a download link but it has the following drawback