-2
#!/bin/bash

while true
do
     if [[ $# -eq 0 ]] ; then
         echo Enter operand1 value:
         read operand1
 
         # Offer choices
         echo 1. Addition
         echo 2. Subtraction
         echo 3. Multiplication
         echo 4. Division
         echo 5. Exit
 
         echo Enter your choice:
         read choice

         if [[ $choice != 1 || 2 || 3 || 4 || 5 ]] ; then
             echo Sorry $choice is not a valid operator - please try again 
             echo Enter your choice:
             read choice
         else 
             Continue 
         fi
 
         echo Enter operand2 value:
         read operand2
 
         # get operands and start computing based on the user's choice
         if [[ $choice -eq 1 ]] ; then
             echo ----------------------------------------
             echo Addition of $operand1 and $operand2 is $((operand1+operand2))
             echo ----------------------------------------
             echo
         elif [[ $choice -eq 2 ]] ; then
             echo ----------------------------------------
             echo Subtraction of $operand1 and $operand2 is $((operand1-operand2))
             echo ----------------------------------------
             echo
         elif [[ $choice -eq 3 ]] ; then
             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    
          elif [[ $choice -eq 4 && operand2 -ne 0 ]] ; then
             echo ----------------------------------------
             echo Division of $operand1 and $operand2 is $((operand1/operand2))
             echo ----------------------------------------
             echo    
         elif [[ $choice -eq 5 ]] ; then
             exit    
         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 am looking to add functionality to the above code so that each subsequent operation will use the previous result, prompt the user for the next operator and operand so that the user doesn't have to enter the first operand again and it simply stores it in memory. I cant seem to think of any ways to do this - any advice?

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • 2
    Q: Do you want to use the same value of "operand1" over and over again ... or do you want to use the *result* as the 1st operand in subsequent operations? In either case: just use a shell variable. *Assign the result to a variable* ... instead of just printing it. [Here](https://stackoverflow.com/a/13864829/3135317) is how you can check if a Bash variable is assigned or not. – FoggyDay Feb 21 '21 at 23:43
  • 1
    Code in a question should be a [mre] -- the **shortest possible thing** that demonstrates a specific problem. Why have five options and a bunch of `echo`s? Beyond that, it's not clear why you're having a problem with storing a value across iterations -- variables you set stay in place, so what's the specific thing that goes wrong when you try? – Charles Duffy Feb 21 '21 at 23:45
  • 1
    BTW, `[[ $choice != 1 || 2 || 3 || 4 || 5 ]]` is always true, because after checking `[[ $choice != 1 ]]` (which may be either true or false), it checks `[[ 2 ]]`, which is always true (because it's equivalent to `[[ -n 2 ]]`, and `2` is not an empty string). – Charles Duffy Feb 21 '21 at 23:46
  • Thanks for pointing that out @CharlesDuffy I see that now. Would you recommend a way to format this I've been trying to input a line which would show the user that they inputted an incorrect operator and this is the only one that actually functions with my script. Any help being pushed in the right direction would be greatly helpful – Luke McCutcheon Feb 22 '21 at 00:12
  • The above aspect of the question is a duplicate of [Compare string to multiple correct values](https://stackoverflow.com/questions/21157435/bash-string-compare-to-multiple-correct-values). – Charles Duffy Feb 22 '21 at 00:24
  • For the other part of the question it's unclear where you got stuck, but if it's in the `if [[ $result = "" ]]` part, that would be a duplicate of [How to find out whether or not a variable is empty in bash?](https://stackoverflow.com/questions/3061036/how-to-find-whether-or-not-a-variable-is-empty-in-bash) – Charles Duffy Feb 22 '21 at 00:24
  • @CharlesDuffy I updated my code to if [[ $choice != 1 && $choice != 2 && $choice != 3 && $choice != 4 && $choice != 5 ]] ; then echo Sorry not a valid operator - please try again echo Enter your choice: read choice else Continue fi which seems to work although I get a message error: WARNING:root:could not open file '/etc/apt/sources.list' although the script still works not too sure whats happening the variable I have been able to make progress on appreciate you putting the info there! – Luke McCutcheon Feb 22 '21 at 00:45
  • Consider `if ! [[ $choice = [1-5] ]]`, btw. – Charles Duffy Feb 22 '21 at 02:22
  • @LukeMcCutcheon : In a computer, things are "memorized" by storing them into a file. Hence use a file to hold the information which you need to keep between two invocation. Don't forget that perhaps several people are using the script alternatively or even at the same time. Maybe even one user may run several instances of the script in parallel. Take this into account when developing your solution. – user1934428 Feb 22 '21 at 08:15

1 Answers1

0

You should store the result in a variable like this:

result=$((operand1+operand2))

Then your first if statement can check if this variable has a value, and if it does, skip the read and use it instead.

if [[ $result == "" ]]; then
    echo Enter operand1 value:
    read operand1
else
    operand1=$result
    echo "Operand 1 is: $result"
    echo ""
fi
...

Here is the working version of your 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 Enter your choice:
         read choice

         if [[ $choice != 1 && $choice != 2 && $choice != 3 && $choice != 4 && $choice != 5 ]] ; 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    
         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
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user122121
  • 130
  • 6