좋아, 모든 코드가 있고 작동합니다. 난 그냥 곤경에 빠졌어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
한번 사용해 보고 어떻게 생각하는지 말해주세요.