我试图编写一个程序,程序会问你你的名字,确认你想要这个名字,然后查看你是否说是。我有"不"循环工作,它会再次要求一个名字,但是我试图在一个循环中,如果你输入除了是或否之外的任何东西,它会说"请输入是或否。"然后询问您是否要确认您的姓名,然后再次检查答案。这是我拥有的。
echo -e $WHITE"Name your$GREEN Hero$WHITE."
read HERO
clear
echo -e "Are you sure you want your$GREEN Hero$WHITE's name to be$GREEN "$HERO"$WHITE? y/n"
read ANSWER1
while [ $ANSWER1 = "no" ]; do
#
#
#
clear
echo -e $WHITE"Name your$GREEN Hero$WHITE."
read HERO
clear
echo -e "Are you sure you want your$GREEN Hero$WHITE's name to be$GREEN "$HERO"$WHITE? y/n"
read ANSWER1
done
while [ $ANSWER1 != "yes" ] || [ $ANSWER1 != "no" ]; do
#
#
#
clear
echo -e $WHITE"Please type yes or no."
sleep 1.5
clear
echo -e $WHITE"Are you sure you want your$GREEN Hero$WHITE's name to be$GREEN "$HERO"$WHITE?"
read ANSWER1
clear
done
clear
echo -e -n $WHITE"Loading"
sleep 1.5
echo -e -n "."
sleep 1.5
echo -e -n "."
sleep 1.5
echo -e -n "."
sleep 1.5
clear
echo -e "Done."
如果我通过"请输入是或否。"它会在睡眠时冻结而不清晰,因此不会显示回声并继续循环。任何帮助或建议将不胜感激!
答案 0 :(得分:2)
问题在于:
while [ $ANSWER1 != "yes" ] || [ $ANSWER1 != "no" ]; do
每个可能的答案至少与其中一个答案不同。没有字符串同时等于yes
和no
。您需要&&
而不是||
。
答案 1 :(得分:0)
编写循环以获取用户输入的更简单方法是select
命令:
while true; do
read -p "${white}Name your ${green}Hero$white: " hero
echo "Are you sure you want your ${green}Hero$white's name to be $green\"$hero\"$white? "
select ans in Yes No; do
case $ans in
Yes) break 2 ;; # break out of both select and while loops
No) break ;; # only break out of select loop
esac
done
done
echo "Your ${green}Hero$white's name is $green\"$hero\"$white."