需要编写一个“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

我看到的第一件事是您的脚本中存在多个“口渴”一词的拼写错误,这导致它无法正常工作。搜索单词“thisrty”并将其替换为正确的单词“口渴”。


另外,我不确定您是否想稍后在代码中添加更多内容,但按照现在的方式,您可以将 while 替换为无限循环,并删除 while 之后的“if” ,因为变量“口渴”的值永远不会再改变,如下所示:

#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

尝试一下并告诉我你的想法。

相关内容