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

Murach's JavaScript and DOM Scripting

by Ray Harris 20 chapters, 764 pages, 337 illustrations ISBN: 978-1-890774-55-4 Today, web users expect web sites to provide advanced features, dynamic user interfaces, and fast response times. To deliver that, you need to know the JavaScript language. Beyond that, though, you need to know how to use JavaScript to script the Document Object Model (or DOM). Now, at last, you can learn both JavaScript and DOM scripting in this one great book. In fact, by the end of chapter 3, you'll be able to develop and debug significant JavaScript applications. By the end of section 2, you'll know the complete JavaScript language and some elementary DOM scripting. And by end of section 3, you will have raised your DOM scripting skills to an expert level.

Murach's Java SE 6

Murach's Java SE 6 by Joel Murach and Andrea Steelman 22 chapters, 810 pages, 351 illustrations ISBN-10: 1-890774-42-1; ISBN-13: 978-1-890774-42-4 "I love your Java book. It cuts right to the essential information, providing the perfect balance between too many details and too little information. I bought another well-known Java book, but I spent several hours trying to learn what I learned from your book in about 45 minutes." That's what one developer wrote us about the JDK 5 edition of our core Java book. Now, you can expect that same practical, time-saving approach in the current edition that covers the latest version of Java, Java SE 6. What you'll learn from the SE 6 edit...

Murach's Java Servlets and JSP (2nd Edition)

by Joel Murach and Andrea Steelman    24 chapters, 729 pages, 329 illustrations ISBN-13: 978-1-890774-44-8 Ever since we published the first edition of this book back in 2003, we've been getting feedback like this from developers: "This book is excellent, excellent, excellent! The authors have a gift for developing the topic of JSP, Servlets, and building a Java-based ecommerce site in a methodical, easy-to-understand approach." Now, I'm delighted to tell you that you'll feel the same way about this new, 2nd edition. The content has been redone from start to finish, so you'll learn the best features, tools, and techniques for developing today's web applications (see what's NEW below).But our popular approach hasn't changed: This 2nd edition still presents Java web programming ...