问题描述
#!/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
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:
解决方法
#!/bin/bash
while true
do
if [[ $result ]] ; then
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))
result=$((operand1+operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 2 ]] ; then
echo ----------------------------------------
echo Subtraction of $operand1 and $operand2 is $((operand1-operand2))
result=$((operand1-operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 3 ]] ; then
echo ----------------------------------------
echo Multiplication of $operand1 and $operand2 is $((operand1*operand2))
result=$((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))
result=$((operand1/operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 5 ]] ; then
exit
else
echo ----------------------------------------
echo Invalid choice.. Please try again
echo ----------------------------------------
echo
fi
done
,
这是一种减少重复代码的方法。请注意使用 select
来呈现菜单。
#!/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