0

Would someone mind giving me a hand with the following IF Statement?

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

If I enter 'yes' I get the following as an endless result

= yes
= yes
= yes
...
= yes

And if I enter 'no' I get the following:

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

Any ideas?

arco444
  • 22,002
  • 12
  • 63
  • 67
  • possible duplicate of [How do I compare two string variables in an 'if' statement in Bash?](http://stackoverflow.com/questions/4277665/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash) – arco444 Jun 29 '15 at 14:02

1 Answers1

2

The output you're getting is the same as if you typed:

yes = 'yes'

To denote a comparison, you should use brackets on the if. It should be:

if [ $REBOOT = 'yes' ] ; then

plus you have a second condition on the else without another if. You don't need it anyway

Total code should be:

if [ $REBOOT = 'yes' ] ; then
   echo 'System will now reboot'
   shutdown -r now
else
   echo 'You have chosen to reboot later'
fi
Shlomi Agiv
  • 1,183
  • 7
  • 17