0

I have a while loop to determine if user entered a new line and will exit once the user enters anything but a new line with the following snippet:

...
while [$abc -e ""]
do
read -p " enter something besides new line " abc
done
...

Say the user presses 'Enter' 4 times followed by 'someNonNewLineString' the output will be

enter something besides new line

enter something besides new line 

enter something besides new line 

enter something besides new line 

line 1 [someNonNewLineString: command not found

from a control perspective, it works the way as expected as this loop is exited once the user enters anything besides 'Enter' but why is that last statement returned saying command not found?

also, is this the right way of approaching this (detect if user entered new line, if yes, re prompt for something else)?

Boboyum
  • 668
  • 3
  • 10
  • 2
    Why are you using `-e` ? That checks if a file exists, use `=` to compare strings. – 123 Mar 04 '16 at 08:53

1 Answers1

1

Use spaces around [ and ], compare using =.

while [ "$abc" = "" ]
  • changing it to while [ "$abc" -e "" ] gives me: [: -e: binary operator expected when I enter 'enter' – Boboyum Mar 04 '16 at 08:46
  • This worked! thanks. guess i got -e , -eq , =, == all mixed up. – Boboyum Mar 04 '16 at 09:00
  • @Boboyum if I were you I'd forget about `==` as it doesn't do anything different to `=` and is non-standard, so only works in some shells like bash. – Tom Fenech Mar 04 '16 at 09:11