![bash 脚本中的语法错误:接近意外的标记“else”](https://linux22.com/image/39727/bash%20%E8%84%9A%E6%9C%AC%E4%B8%AD%E7%9A%84%E8%AF%AD%E6%B3%95%E9%94%99%E8%AF%AF%EF%BC%9A%E6%8E%A5%E8%BF%91%E6%84%8F%E5%A4%96%E7%9A%84%E6%A0%87%E8%AE%B0%E2%80%9Celse%E2%80%9D.png)
#!/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
语句解析参数而不是读取输入。