!["thirsty.sh"スクリプトを書く必要があります:BASH [閉じる]](https://linux33.com/image/75226/%22thirsty.sh%22%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%82%92%E6%9B%B8%E3%81%8F%E5%BF%85%E8%A6%81%E3%81%8C%E3%81%82%E3%82%8A%E3%81%BE%E3%81%99%EF%BC%9ABASH%20%5B%E9%96%89%E3%81%98%E3%82%8B%5D.png)
さて、すべてのコードがあり、動作します。私はただ困っています。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
答え2
ユーザー(プレイヤー?)に入力を要求する機能を使用するようにコードを変更できます。
is_thirsty() {
echo """
Are you thirsty (Yes/No)?'
"""
while :
do
read -p '>' thirsty
case ${thirsty^^} in
NO|N)
return 1
;;
YES|Y)
return 0
;;
*)
echo -n '(Yes/No)'
;;
esac
done
}
使用例は次のとおりです。
choose_drink() {
echo """
Okay, what would you like to drink?
We have: water, beer, wine and anything else you can think of.
"""
read -p '>' drink
case ${drink^^} in
WATER)
echo "Clear crisp and refreshing"
;;
BEER)
echo "Let me see some ID"
;;
WINE)
echo "One box or two?"
;;
*)
echo "Coming right up..."
;;
esac
}
goodbye() {
echo "Okay, thank you for coming. Have a nice day."
}
is_thirsty && choose_drink || goodbye
答え3
こんなものを探していると思います。
# Make thirsty an uppercase variable
typeset -u thirsty
# Initialize thirsty
thirsty="INIT"
while [ "$thirsty" != "YES" || "$thirsty" != "Y" || "$thirsty" != "NO" || "$thirsty" != "N" ]
do
#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" == "N" ]; 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" == "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
done
一度使ってみて、どう思うか教えてください。