![读取/如果命令帮助](https://linux22.com/image/857751/%E8%AF%BB%E5%8F%96%2F%E5%A6%82%E6%9E%9C%E5%91%BD%E4%BB%A4%E5%B8%AE%E5%8A%A9.png)
我正在尝试运行这个-
#!/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
if
需要then
和fi
- 不过
else [test]
elif [test]
- 不需要分组括号
答案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