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

"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

答え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

一度使ってみて、どう思うか教えてください。

関連情報