Question
I have the below UNIX code: #!/bin/bash while true do if [[ $# -eq 0 ]] ; then if [[ $result == ]]; then
I have the below UNIX code:
#!/bin/bash
while true
do
if [[ $# -eq 0 ]] ; then
if [[ $result == "" ]]; then # check if $result is empty
echo Enter operand1 value:
read operand1
else
operand1=$result # if not empty then assign its value to $operand1
echo "Operand 1 is: $result"
echo ""
fi
# Offer choices
echo 1. Addition
echo 2. Subtraction
echo 3. Multiplication
echo 4. Division
echo 5. Exit
echo MR
echo 7. MS
echo 8. MC
echo 9. M+
echo 10. Clear
echo Enter your choice:
read choice
if [[ $choice != 1 && $choice != 2 && $choice != 3 && $choice != 4 && $choice != 5 && $choice != MR ]] ; then
echo Sorry $choice is not a valid operator - please try again
echo Enter your choice:
read choice
fi
echo Enter operand2 value:
read operand2
# get operands and start computing based on the user's choice
if [[ $choice -eq 1 ]] ; then
result=$((operand1+operand2)) # store result
echo ----------------------------------------
echo Addition of $operand1 and $operand2 is $((operand1+operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 2 ]] ; then
result=$((operand1-operand2)) # store result
echo ----------------------------------------
echo Subtraction of $operand1 and $operand2 is $((operand1-operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 3 ]] ; then
result=$((operand1*operand2)) # store result
echo ----------------------------------------
echo Multiplication of $operand1 and $operand2 is $((operand1*operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 4 && operand2 -eq 0 ]] ; then
echo Can not divide by 0 please try again
echo Please enter operand2
read operand2
echo ----------------------------------------
echo Division of $operand1 and $operand2 is $((operand1/operand2))
echo ----------------------------------------
echo
result=$((operand1/operand2)) # store result
elif [[ $choice -eq 4 && operand2 -ne 0 ]] ; then
result=$((operand1/operand2)) # store result
echo ----------------------------------------
echo Division of $operand1 and $operand2 is $((operand1/operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 5 ]] ; then
exit
elif [[ $choice || $operand1 || $operand2 -eq MR ]] ; then
echo MR $result
else
echo ----------------------------------------
echo Invalid choice.. Please try again
echo ----------------------------------------
echo
fi
else
echo ----------------------------------------
echo You either passed too many parameters or too less
echo than the optimum requirement.
echo
echo This program accepts a maximum of 2 arguments or no
echo argument at all in order to run successfully.
echo ----------------------------------------
fi
done
I have attempted to add functionality that include MR, MS, MC and M+ although I have been struggling to add it in. Would someone be able to help with this?
The requirement is to have a calculator which asks a user an operand then the operator then the second operand. The calculator is to operate so that the result is always stored. Meaning that the user will not have to enter the first operand as it will simply be from the previous result. I have everything but the functionality for MR, MS, MC M+ and clear set up so that it runs I just need to implement this functionality which I am stuck on.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started