Shell 脚本 - 添加内存功能 [英] Shell Script - Adding memory functionality
问题描述
#!/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?
推荐答案
您应该将结果存储在这样的变量中:
You should store the result in a variable like this:
result=$((operand1+operand2))
然后你的第一个 if
语句可以检查这个变量是否有值,如果有,跳过 read
并使用它.
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
这篇关于Shell 脚本 - 添加内存功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!