Bash- if clause doesn't work correctly -


this question has answer here:

i want make shutdown script , doesn't work intended. wrote.

echo "wanna shutdown (y/n)?"   read answer  if [ $answer=="y" ]              sudo shutdown -p else      printf "something...." 

whatever press shuts down. why?

you need put spaces around == operator. otherwise test expression single word, , non-empty words test successfully.

in addition, if wish portable, should use = instead of ==. , wise double quote variable expansions because [ won't you.

if [ "$answer" = y ]; 

on other hand, if using bash (or ksh or zsh) use more forgiving [[ conditional expression:

if [[ $answer = y ]]; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -