1

I want to make a shutdown script and it doesn't work as intended. This is what I wrote.

echo "Wanna shutdown (y/n)?"

 read ANSWER

if [ $ANSWER=="y" ]
     then
        sudo shutdown -P now
else 
    printf "Something...."

whatever i press it just shuts down. Why?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Ryncops
  • 189
  • 1
  • 14

1 Answers1

2

You need to put spaces around the == operator. Otherwise the test expression is a single word, and all non-empty words test successfully.

In addition, if you wish to be portable, you should use = instead of ==. And it is always wise to double quote variable expansions because [ won't do that for you.

if [ "$ANSWER" = y ]; then

On the other hand, if you are using bash (or ksh or zsh) you could use the more forgiving [[ conditional expression:

if [[ $ANSWER = y ]]; then
rici
  • 234,347
  • 28
  • 237
  • 341
  • Indeed that was the mistake. Thank you! I need to wait 7 minutes before accepting this answer, so I will do so as soon as the time passes. – Ryncops Sep 29 '15 at 20:59