bash if语句问题

bash if statement issue

有人介意帮我处理以下 IF 语句吗?

read -e -p "Would you like to reboot now?: " -i " " 重启

if $REBOOT = 'yes' ; then
   echo 'System will now reboot'
   shutdown -r now
else $REBOOT != 'yes'
   echo 'You have chosen to reboot later'
fi

如果我输入 'yes' 我会得到以下无穷无尽的结果

= yes
= yes
= yes
...
= yes

如果我输入 'no',我会得到以下信息:

./test.sh: line 7: no: command not found
./test.sh: line 10: no: command not found
You have chosen to reboot later

有什么想法吗?

您获得的输出与您输入的输出相同:

yes = 'yes'

要表示比较,您应该在 if 上使用方括号。应该是:

if [ $REBOOT = 'yes' ] ; then

加上你在 else 上有第二个条件,没有另一个 if。反正你不需要它

总代码应为:

if [ $REBOOT = 'yes' ] ; then
   echo 'System will now reboot'
   shutdown -r now
else
   echo 'You have chosen to reboot later'
fi