linux - bash if 语句问题

标签 linux bash if-statement scripting

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

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

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

如果我输入"is",我会得到以下无尽的结果

= yes
= yes
= yes
...
= yes

如果我输入“否”,我会得到以下信息:

./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

关于linux - bash if 语句问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31117622/

相关文章:

linux - 如何在 linux 系统上搜索包含文本字符串的文本文件

linux - shell如何为awk生成输入

mysql - 在 Ubuntu 上纯粹从 bash 脚本安装 mysql 5.7

javascript - 带有 if 异常行为的控制台日志

C++ cout 多次打印出相同的字符串

python-3.x - 如何知道 pandas 列中的每个后续值是否都大于前面的值? Python相关

python - 无法使用 Supervisord 启动 Python 应用程序

linux原始以太网套接字绑定(bind)到特定协议(protocol)

linux - 全局文件到 LXC 容器?

python - Systemd 看门狗通过脚本运行程序