我對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條經驗 獲得超6個贊
您需要在代碼中修復很多事情。我強烈建議您先閱讀基礎教程,然后再繼續。
無論如何,您可以做的最基本的程序是:
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 關注
- 314 瀏覽
添加回答
舉報
0/150
提交
取消