如果输入错误,如何不继续脚本(Shell Script Putty)

如果输入错误,如何不继续脚本(Shell Script Putty)

我编写了一个简单的点餐程序,对于那些只有几天时间来学习这个的人来说,我已经取得了很大的进步。 (考虑到我根本不是程序员)。我希望程序的第一部分允许“订购”或“订购”,然后允许“退出”或“退出”,这样效果很好。然后我希望它有一条消息,表明如果两者都没有输入,则输入无效。解决这个问题的最佳方法是什么?这是在 Shell 脚本中

clear
read -rp "Do you want to order or quit?" start
    if echo "$start" | grep -iq 'order'; then
    continue
    elif echo "$start" | grep -iq 'quit'; then
    exit 0
    else echo "Please order or quit";
fi

echo "Please enter your name"
read name
echo "What is your phone number"
read number
echo "What kind of fruit do you want?"
read fruit
echo "How much KG do you want?"
read kg

echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
echo  "Hello" $name" here is a summary of your order:"
date
echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
echo "Your phone number is:" $number
echo "You have ordered" $kg "kg of" $fruit
echo $fruit "is £4 a kilogram, so you will pay"
expr 4 \* $kg

while true; do
read -rp 'Would you like to order again?' order
if echo "$order" | grep -iq 'yes'; then
    exec $0
elif echo "$order" | grep -iq 'no'; then
     exit 0
else echo "Please say Yes or No";
fi
done

答案1

你的剧本做得很好。您可以检查两个输入是否都有效,如下所示:

# change lowercase to uppercase
start=`echo $start | tr "[a-z]" "[A-Z]"` 

# exit if start isn't either "ORDER" or "QUIT"
if [ $start != "ORDER" ] && [ $start != "QUIT" ]
then
    echo "Your input was not correct"
    exit
fi

相关内容