#!/bin/bash #initiate continueCalculator variable to 1 continueCalculator=1 #start loop for calculator while [ $continueCalculator = 1 ] do #ask user for first number echo "Please enter the first value " #get value read firstNumber #ask user for second number echo "Please enter the second value " #get value read secondNumber #ask user what operation they want to use and list all 4 options echo "Which operation would you like to use? Enter 1,2,3 or 4." echo 1 "1 for addition" echo 2 "2 for subtraction" echo 3 "3 for multiplication" echo 4 "4 for division" # get operation from user read operation #go through case until you reach operation needed case $operation in # calculate for addition 1)answer=$(($firstNumber + $secondNumber)) #output answer echo "You entered $firstNumber and $secondNumber. The sum is: $answer";; # calculate for subtraction 2)answer=$(($firstNumber - $secondNumber)) #output answer echo "You entered $firstNumber and $secondNumber. The difference is: $answer";; # calculate for multiplication 3)answer=$(($firstNumber * $secondNumber)) #output answer echo "You entered $firstNumber and $secondNumber. The product is: $answer";; # is the denominator 0? 4)if (($secondNumber == 0)); then echo "You entered $firstNumber and $secondNumber. You cannot divide by 0"; else # denominator not 0. calculate for division answer=$(($firstNumber / $secondNumber)) # calculate remainder remainder=$(($firstNumber % $secondNumber)) #output answer echo "You entered $firstNumber and $secondNumber. The quotient is: $answer with a remainder of $remainder"; fi esac # continue loop while continue == yes echo "Press 1 to continue, press 0 to stop" # get answer if user wants to continue read continueCalculator # did user enter 0? if (($continueCalculator == 0)); #exit the loop then exit fi done
Write, Run & Share Bash code online using OneCompiler's Online Bash Shell for free. It's one of the robust, feature-rich Bash shell available over online and getting started with the OneCompiler's Bash Shell is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Bash
. OneCompiler also has reference scripts, where you can look for the sample scripts and start coding.
Bash (Bourne Again Shell) is a shell program written by Brian Fox and is an upgraded version of Bourne Shell program 'sh'.
name="Foo"
echo $name
echo "$name"
echo "${name}"
if [ conditional-expression ]
then
statements
fi
if [ conditional-expression ]
then
statements
else
statements
fi
if [ conditional-expression ]
then
statements
elif [ conditional-expression ]
then
statements
else
statements
fi