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

How To Remove “Error Deleting File or Folder”

It is a very common and annoying message that the file or folder you are trying to deleted cannot be deleted or thependrive cannot be removed as it is used by the system.In this tutorial I will show how to remove your pendrive or delete the files and folders if you get such an error.This error has various flavours and it can be of any of the following types:

What is the term Multiplexing in data transmission ?

  Multiplexing is the technique of combining multiple signals into a single signal for transmission over a communication channel. In data transmission, multiplexing allows multiple users to share a single communication channel, maximizing its use and increasing efficiency. There are several types of multiplexing used in data transmission, including: Frequency Division Multiplexing (FDM): This technique divides the available frequency range into multiple non-overlapping sub-channels, each of which can be used by a separate signal. Each signal is modulated onto a separate carrier frequency and combined into a single composite signal for transmission. FDM is commonly used in analog systems, such as radio and television broadcasting. Time Division Multiplexing (TDM): This technique divides the available time into multiple time slots, with each slot dedicated to a separate signal. Each signal is transmitted in its designated time slot, and the signals are interleaved in time to create a...

Murach's Oracle SQL and PL/SQL

Murach's Oracle SQL and PL/SQL by Joel Murach 18 chapters,627 pages, 271illustrations ISBN: 978-1-890774-50-9   If you're developing applications that access Oracle databases, why not let SQL do more of the work for you? With the new, free, Express Edition of Oracle Database and Oracle SQL Developer (also free), you can set up the database environment you need and train yourself right on your own computer. This new book shows you how! Get started fast with Oracle SQL Developer In chapter 1, you'll quickly master the database concepts you have to know to handle Oracle databases effectively. Then, chapter 2 gets you started using Oracle SQL Developer. This graphical tool makes it far easier to work with an Oracle database than it is with command-line tools like SQL*Plus,...