我对Bash脚本非常陌生,所以如果问题有点不一致,请原谅。如果用户回答否,我希望我的脚本重复一个问题4次,如果用户回答是,那么脚本可以退出,这是我到目前为止的内容#!/bin/bashecho "Would you like a cup of tea?"read answerwhile true;do if [ $answer = Y ] then echo "Great, I'll make tea now"; then break if [ $answer = N ] then echo "Are you sure?" continue if [ $answer = N ] then echo "Are you sure?" continue if [ $answer = N ] then echo "Are you sure?" continue if [ $answer = N ] then echo "Ok, I give up!" exitfi
1 回答
梦里花落0921
TA贡献1772条经验 获得超5个赞
您需要在代码中修复很多事情。我强烈建议您先阅读基础教程,然后再继续。
无论如何,您可以做的最基本的程序是:
count=0
while true;
do
read -r answer
if [ "$answer" = "Y" ]; then
echo "Great, I'll make tea now"
break
fi
if [ "$answer" = "N" ]; then
echo "Are you sure?"
count=$((count+1))
if [ $count = 4 ]; then
break
fi
fi
done
我们将其设置count为用户提供“ N”作为答案的次数,并检查它何时达到4,然后中断。
- 1 回答
- 0 关注
- 280 浏览
添加回答
举报
0/150
提交
取消