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

Facts to Consider when purchasing a Hardware Firewall

  When buying a hardware firewall, there are several factors you should consider. Here is a checklist of important things to consider: Performance: Determine the required throughput and session capacity to meet the needs of your network. Security features: Look for features such as intrusion detection and prevention, VPN support, content filtering, anti-virus, anti-spam, and application control. Ease of use: Make sure the firewall has a user-friendly interface for easy configuration and management. Scalability: Consider the ability to add or upgrade hardware components to accommodate future growth. Integration: Determine how well the firewall integrates with your existing network and security infrastructure. Support: Look for a vendor with a good reputation for providing timely and effective support. Price: Evaluate the total cost of ownership, including hardware, licensing, and ongoing maintenance and support costs. Compliance: Check if the firewall meets any regulatory or complia...

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:

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,...