0

I have provide the full script below. I am having trouble with this specific section:

     elif [[ $choice -eq MS ]] ; then
         echo ms -> $result 

     elif [[ $choice -eq MR ]] ; then
         operand2=$result
         echo MR -> $operand2

     elif [[ $choice -eq M+ ]] ; then
         Echo M+ -> $((result+MS)) 

     elif [[ $choice -eq MC ]] ; then
         $result=0
         echo MC -> 0

I am look to incorporate the MR, MS, M+ and MC of a calculator in my unix script. When I run my code I don't get the intended action ie MS does not store the result. Anyone see any reason or structural issues with the above section?

#!/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 MS
         echo MR
         echo M+
         echo MC
 
         echo Enter your choice:
         read choice

         if [[ $choice != 1 && $choice != 2 && $choice != 3 && $choice != 4 && $choice != 5 && $choice != MS && $choice != MR && $choice != M+ && $choice != MC ]] ; 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 -eq MS ]] ; then
             echo ms -> $result 

         elif [[ $choice -eq MR ]] ; then
             operand2=$result
             echo MR -> $operand2

         elif [[ $choice -eq M+ ]] ; then
             Echo M+ -> $((result+MS)) 

         elif [[ $choice -eq MC ]] ; then
             $result=0
             echo MC -> 0
         
         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
  • See also [here](https://stackoverflow.com/q/66321888/3266847) and [here](https://stackoverflow.com/q/66308308/3266847)... – Benjamin W. Feb 22 '21 at 21:24
  • What exactly are you trying to store, and where? What do you think `echo ms -> $result` does? – ilkkachu Feb 22 '21 at 21:37
  • I'm trying to have the result stored in MS and thought that the MS -> $result would store the result in MS – Luke McCutcheon Feb 22 '21 at 21:42
  • In MS what? A variable in the shell? Something else? Elsewhere you're using e.g. `result=$((operand1+operand2))`, to store a value in a variable, why is it `->` now? Are you familiar with the shell's redirection operators? (e.g. https://mywiki.wooledge.org/BashGuide/InputAndOutput#File_Redirection) Where did you pick up that `->` anyway? – ilkkachu Feb 22 '21 at 21:46

0 Answers0