有人介意帮我处理以下 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/