diff --git a/bash/calculator.bash b/bash/calculator.bash new file mode 100755 index 0000000..c7d7787 --- /dev/null +++ b/bash/calculator.bash @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +# Ensure we are given three parameters +if [ $# -ne 3 ]; then + echo "Please run this script like: ./$0 " + echo "where operator is either +, -, * or /. You may need to escape or" + echo "single quote it." + exit 1 +fi + +# Store the operands and operator +operand1=$1 +operator=$2 +operand2=$3 + +# The main case/switch which was required in the exercise +case $operator in +'+') + result=$((operand1 + operand2)) + operatorWord="plus" + ;; +'-') + result=$((operand1 - operand2)) + operatorWord="minus" + ;; +'*') + result=$((operand1 * operand2)) + operatorWord="multiplied by" + ;; +'/') + if [ "$operand2" -eq 0 ]; then + echo "The laws of mathematics don't allow division by zero." + exit 1 + else + result=$((operand1 / operand2)) + operatorWord="divided by" + fi + ;; +# Let's not forget default case either +*) + echo "Please use operator +, -. * or /" + exit 1 + ;; +esac + +# and finally the output in format wanted by the task +echo "$operand1 $operatorWord $operand2 equals $result"