bash 脚本中的语法错误:接近意外的标记“else”

bash 脚本中的语法错误:接近意外的标记“else”
#!/bin/bash

input=""

echo "Does a wall needs to be sent?"
read input

if [ $input="yes" ]; then
   echo "Sending message to all users"
   echo ""
else if [ $input="no"]; then
    exit
    fi
fi
echo "Is this a reboot or shutdown?"
      read input
if [ $input="reboot" ]; then
   reboot
elif [ $input="shutdown" ]; then
else
echo ""
echo "Goodbye"

答案1

该脚本有很多问题。这是一个清理后的版本:

#!/usr/bin/env bash

input=""

echo "Does a wall needs to be sent?"
read input

if [ "$input" = "yes" ]; then
    echo "Sending message to all users\n"
elif [ "$input" = "no" ]; then
    exit
fi

echo "Is this a reboot or shutdown?"
read input

if [ "$input" = "reboot" ]; then
    reboot
elif [ "$input" = "shutdown" ]; then
    shutdown -h now
fi

echo "\nGoodbye"

不过说实话,这件事还是做得很差。我建议使用case语句解析参数而不是读取输入。

相关内容