0

I'm trying to run this-

#!/bin/bash

echo "Who are you?"
read NAMES

{
if [ "$NAMES" == "Mallory" ] ; 
    echo "Hello, me!"
else [ "$NAMES" == "Palmer" ] ; 
    echo "Hey, baby! I love you!"
else echo "Gtfo here..."
}

But every time I do, this happens...

mallory@whitecelica:~/stuff/learning/internets$ ./wru
Who are you?
Mallory
./wru: line 9: syntax error near unexpected token `else'
./wru: line 9: `else [ "$NAMES" == "Palmer" ] ; '

What am I doing wrong?

1
  • I'm sure there are those here better qualified than myself to answer this but I believe the syntax runs if [this], else [that] fi I don't think the second else is valid, logically, you've already covered all possibilities with the first else. you only get Hello, me if it's Mallory, you get else if it's not. you might test if it's Mallory OR Palmer first, else a new if else fi group testing which of the names you got
    – Elder Geek
    Jul 10, 2014 at 0:40

2 Answers 2

1
read -p "Who are you? " NAMES

if [[ "$NAMES" == "Mallory" ]] ; then
    echo "Hello, me!"
elif [[ "$NAMES" == "Palmer" ]] ; then
    echo "Hey, baby! I love you!"
else
    echo "Gtfo here..."
fi
  1. if requires then and fi
  2. not else [test] but elif [test]
  3. your grouping braces are not needed
0
0

I got 4 fewer characters:

#!/bin/bash

echo "Who are you?"
read NAMES


if [ "$NAMES" == "Mallory" ] ; then
    echo "Hello, me!"
elif [ "$NAMES" == "Palmer" ]; then
    echo "Hey, baby! I love you!"
else echo "Gtfo here..."
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .