我對Bash腳本非常陌生,所以如果問題有點(diǎn)不一致,請?jiān)?。如果用戶回答否,我希望我的腳本重復(fù)一個問題4次,如果用戶回答是,那么腳本可以退出,這是我到目前為止的內(nèi)容#!/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貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個贊
您需要在代碼中修復(fù)很多事情。我強(qiáng)烈建議您先閱讀基礎(chǔ)教程,然后再繼續(xù)。
無論如何,您可以做的最基本的程序是:
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
我們將其設(shè)置count為用戶提供“ N”作為答案的次數(shù),并檢查它何時達(dá)到4,然后中斷。
添加回答
舉報
0/150
提交
取消