#!/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
이 코드의 맨 위 부분에서 특별히 작업하고 있습니다. 사용자의 결과가 다음 operand1에 대해 이월되도록 노력하고 있습니다. 각 계산 후 operand1 = 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:
다음은 중복 코드를 줄이는 방법입니다. 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
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다