1

So I create a script and it works perfect except at the end when I type in a beverage it executes a line its not suppose to. The Last line is only suppose to come up when I type "no" or "No"...What did I do wrong?

echo -n "Are you thirsty?"
read th

if [ "$th" = "yes" ] || [ "Yes" ]; then
    echo "What would you like to drink?"
    read th
fi

if [ "$th" = "water" ]; then
    echo "Clear crisp and refreshing."
elif [ "$th" = "beer" ]; then
    echo "Let me see some ID."
elif [ "$th" = "wine" ]; then
    echo "One box or Two?"
else
    echo "Coming right up."
fi

if [ "$th" = "no" ] || [ "No" ]; then
    echo "Come back when you are thirsty."
fi
5
  • So it's executing the last if statement (the one with no) regardless of your input? Commented Mar 23, 2017 at 23:09
  • yes, if i type in beer, it gives the echo for it and the "come back when you are thirsty" at the same time Commented Mar 23, 2017 at 23:10
  • I recommend including the "no" option as an else block in the first if statement. Commented Mar 23, 2017 at 23:11
  • let me try that out Commented Mar 23, 2017 at 23:12
  • so i did that and now when I ask are you thirsty and I type no, it still asks me what would I like to drink Commented Mar 23, 2017 at 23:22

3 Answers 3

4

Your problem is that [ "Yes" ] and [ "No" ] are equivalent to [ -n "Yes" ] and [ -n "No" ] and therefore always evaluate to true.

The proper syntax would be:

if [ "$th" = "yes" ] || [ "$th" = "Yes" ]; then
...
if [ "$th" = "no" ] || [ "$th" = "No" ]; then

Or:

if [ "$th" = "yes" -o "$th" = "Yes" ]; then
...
if [ "$th" = "no" -o "$th" = "No" ]; then

Or, if you are using bash as a Bourne shell interpreter:

if [ "${th,,}" = "yes" ]; then
...
if [ "${th,,}" = "no" ]; then

(${th,,} being substituted with the lower case value of variable th)

5
  • oh man I didnt even notice that, I have been at this script since 3:30 eastern time. So I changed it and it works but the last echo that says "Coming right up." prints along with the echo when I say no. Which would be the last variable in the second if Commented Mar 23, 2017 at 23:37
  • is there anything you would recommend for me to read or research on for scripting language that may help in the future? Commented Mar 23, 2017 at 23:41
  • To answer your first comment: if you say No, then the else statement is executed, that's normal. You should put your if [ "$th" = "water" ]...fi block inside the if [ "$th" = "yes" ]...fi block. And no, sorry, my bedside book is the bash manual, but that's not the most didactic reading. The answers here are a good reading though. Commented Mar 23, 2017 at 23:56
  • @E.Mart For reading you can check this one: mywiki.wooledge.org/BashFAQ and also stack overflow documentation: stackoverflow.com/documentation/bash/topics Commented Mar 24, 2017 at 0:46
  • Thank you, I really appreciate it. Ill definitely look into that, scripting is no joke lol. Commented Mar 24, 2017 at 0:55
2

Your tests aren't doing what you think they're doing.

if [ "$var" = "value" ] || [ "Value" ];

This does not do two equality tests. It checks the first case, and then, if that fails, checks to see whether "Value" exists, which it does, because it is there to check. So it always passed through to the then corresponding with the if. You probably want:

if [ "$var" = value" ] || [ "$var" = "Value" ]

Better still might be to look into a case..esac block:

case "$var" in
    value|Value)
        do_stuff
        ;;
    other|Other)
        do_something_else
        ;;
esac
2
  • is there anything you would recommend for me to read or research on for scripting language that may help in the future? Commented Mar 23, 2017 at 23:41
  • I would definitely recommend tldp.org/LDP/abs/html Commented Mar 24, 2017 at 16:12
0

(1) When you test for [ "Yes" ] and later for [ "No" ], you need to still compare it against th in both parts:

[ "$th" = "yes" ] || [ "$th" = "Yes" ]

and

[ "$th" = "no" ] || [ "$th" = "No" ]

(2) For the section, if [ "$th" = "yes" ] || [ "$th" = "Yes" ]; , you need to extend this code block to include everything up to the test for No and use an elif at that point to combine it as one larger compound if-elif-fi statement.

Here it is with the corrections mentioned above:

echo -n "Are you thirsty?"
read th

if [ "$th" = "yes" ] || [ "$th" = "Yes" ]; then

    echo "What would you like to drink?"
    read th

    if [ "$th" = "water" ]; then
      echo "Clear crisp and refreshing."
    elif [ "$th" = "beer" ]; then
      echo "Let me see some ID."
    elif [ "$th" = "wine" ]; then
      echo "One box or Two?"
    else
      echo "Coming right up."
    fi

elif [ "$th" = "no" ] || [ "$th" = "No" ]; then
    echo "Come back when you are thirsty."
fi

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.