#!/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

Я хочу добавить функциональность к приведенному выше коду, чтобы каждая последующая операция использовала предыдущий результат, предлагала пользователю следующий оператор и операнд, чтобы пользователю не приходилось снова вводить первый операнд, и он просто сохранял его в объем памяти. Кажется, я не могу придумать никаких способов сделать это - какой-нибудь совет?

-2
Luke McCutcheon 22 Фев 2021 в 02:35

1 ответ

Лучший ответ

Вы должны сохранить результат в такой переменной:

result=$((operand1+operand2))

Затем ваш первый оператор if может проверить, имеет ли эта переменная значение, и если да, пропустите read и используйте его вместо этого.

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

Вот рабочая версия вашего кода:

#!/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
0
John Kugelman 22 Фев 2021 в 17:51