【问题标题】:bash if statement issuebash if 语句问题
【发布时间】:2015-09-16 00:44:40
【问题描述】:

有人介意帮助我了解以下 IF 语句吗?

read -e -p "现在要重启吗?:" -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

如果我输入“是”,我会得到以下无休止的结果

= 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

有什么想法吗?

【问题讨论】:

标签: linux bash if-statement scripting


【解决方案1】:

你得到的输出和你输入的一样:

yes = 'yes'

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

if [ $REBOOT = 'yes' ] ; then

加上你在没有另一个 if 的情况下对 else 有第二个条件。反正你不需要它

总代码应该是:

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

【讨论】:

    猜你喜欢
    • 2019-08-15
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 2021-05-15
    • 2012-11-19
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多