This is essentially part a calculator in UNIX script. In this part of the script, using MR, MS, or M+ will yield different results. Here is the code in question:
while true
do
echo 'please enter operand'
read operand2
if [ "$operand2" = "X" ]
then
exit
fi
case "$operand2" in
"MS")
(mem=$sum)
echo "$sum -> M"
continue;;
'MR')
(operand2=$mem)
echo $operand2
echo "M -> $mem"
break;;
'M+')
temp=$(( sum+mem ))
echo "$sum + $mem = $temp"
mem=$(( sum+mem ))
continue;;
*[0-9]*)
break;;
*)
echo "Sorry, $operand2 is not a valid input"
continue;;
esac
done
The problem with the code is that with the input of "MR", the variable operand2
remains assigned to the string "MR" rather than being reassigned to the value stored in the mem
variable.
I have tried adding an echo
statement for operand2
, as I thought this may be a scope issue, however, the echo returns "MR" anyways. I have also tried moving around the reassignment within the case, which doesn't seem to resolve the issue.