本文介绍了Shell Scripting - Shell 变量没有结转价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!/bin/bash

while true
do
 if [[ $# -eq 0 ]] ; then

    result=$((operand1+operand2))
    result=$((operand1-operand2))
    result=$((operand1*operand2))

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

     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 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

         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

我专门处理这段代码的顶部.我正在尝试使用它,以便用户的结果可以用于下一个操作数 1.每次计算后我都会得到操作数 1 = 0.知道如何解决这个问题吗?操作数 1 为:0

I am working specifically on the top portion of this code. I am trying to have it so the user's result will carry forward for the next operand1. I get getting operand1 = 0 after each calculation. Any idea how to fix this?operand 1 is: 0

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice:
1
Enter operand2 value:
1
----------------------------------------
Addition of 0 and 1 is 1
----------------------------------------

operand 1 is: 0

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice:

推荐答案

这是一种减少重复代码的方法.注意使用 select 来显示菜单.

Here's a way to reduce the duplicated code. Note the use of select to present the menu.

#!/bin/bash

PS3="Enter the operation: "     # for the select statement

operand() {
    read -r -p "Enter operand${1} value: "
    echo "$REPLY"
}

operand1=$(operand 1)

while true; do
    select operation in Addition Subtraction Multiplication Division Exit; do
        case $operation in
            Exit) exit ;;
            Addition)       op="+"; break ;;
            Subtraction)    op="-"; break ;;
            Multiplication) op="*"; break ;;
            Division)       op="/"; break ;;
        esac
    done

    while true; do
        operand2=$(operand 2)
        if [[ $operation == "Division" ]] && (( operand2 == 0 )); then
            echo Can not divide by 0 please try again 
        else
            break
        fi
    done

    (( result = operand1 $op operand2 ))

    echo "----------------------------------------"
    echo "$operation of $operand1 and $operand2 is $result"
    echo "----------------------------------------"

    echo "operand 1 is: $result" 
    operand1=$result
done

这篇关于Shell Scripting - Shell 变量没有结转价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:57