"thirsty.sh"スクリプトを書く必要があります:BASH [閉じる]

さて、すべてのコードがあり、動作します。私はただ困っています。while loop

#asking the user if they are "thirsty". 
echo "Are you thirsty?"


#creating thirsty variable
read thirsty


#if user answers 'no'/'No'/'NO'/'n'/'N'.
if [ "$thirsty" == "no" ] || [ "$thisrty" == "No" ] || [ "$thisrty" == "N" ] || [ "$thisrty" == "n" ] || [ "$thisrty" == "NO" ]; then
    echo "Okay, thank you for coming. Have a nice day."
    exit
fi

#if user answers 'yes'/'Yes'/'YES'/'y'/'Y'
while [ "$thirsty" != "yes" ]; do

    if [ "$thirsty" == "yes" ] || [ "$thisrty" == "Yes" ] || [ "$thisrty" == "YES" ] || [ "$thisrty" == "y" ] || [ "$thisrty" == "Y" ]; then
        echo "Okay, what would you like to drink?"
        echo "We have: water, beer, wine, and anything else you can think of."
        read drink
        if [ "$drink" == "water" ]; then
            echo "Clear crisp and refreshing"
        elif [ "$drink" == "beer" ]; then
            echo "Let me see some ID"
        elif [ "$drink" == "wine" ]; then
            echo "One box or two?"
        else 
            echo "Coming right up..."
    fi
fi

done

「はい」または「いいえ」のいずれかに答えない場合、スクリプトを再起動するにはwhileループが必要です。

ベストアンサー1

私が最初に見たのは、スクリプトに「thirst」という単語の綴りが複数あり、正しく機能しないということです。 「thisrty」という単語を検索し、正しい単語「thirst」に置き換えてください。


また、後でコードに追加したいかどうかはわかりませんが、今は変数の値のためにwhileを無限ループに置き換えてwhileの後ろの「if」を削除することができます。 「thirst」は次のように再び変わりません。

#asking the user if they are "thirsty". 
echo "Are you thirsty?"
#creating thirsty variable
read thirsty
#if user answers 'no'/'No'/'NO'/'n'/'N'.
if [ "$thirsty" == "no" ] || [ "$thirsty" == "No" ] || [ "$thirsty" == "N" ] || [ "$thirsty" == "n" ] || [ "$thirsty" == "NO" ]; then
    echo "Okay, thank you for coming. Have a nice day."
    exit
fi

while [ 1 ]; do
    echo "Okay, what would you like to drink?"
    echo "We have: water, beer, wine, and anything else you can think of."
    read drink
    if [ "$drink" == "water" ]; then
        echo "Clear crisp and refreshing"
    elif [ "$drink" == "beer" ]; then
        echo "Let me see some ID"
    elif [ "$drink" == "wine" ]; then
        echo "One box or two?"
    else 
        echo "Coming right up..."
    fi
done

おすすめ記事