读取/如果命令帮助

读取/如果命令帮助

我正在尝试运行这个-

#!/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..."
}

但每次我这样做的时候,都会发生这种情况......

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" ] ; '

我究竟做错了什么?

答案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需要thenfi
  2. 不过else [test]elif [test]
  3. 不需要分组括号

答案2

我少了 4 个字符:

#!/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

相关内容