0

My code below:

echo "====================================="
echo "      Test Programme                 "
echo "====================================="
echo 

read -p "Enter Name: " name


if [$name -eq ""]; then
    sleep 1
    echo "Oh Great! You haven't entered name."
    exit
fi

read -p "Enter age:  " age

According to that code,I expected "Oh Great! You haven't entered name." to show up when user skips entering the name which WORKS WELL

But, when you enter a proper string for name, it gives this message/ error:

./cool_ham.sh: line 13: [Franco: command not found

I want to know the reason for that. I have even tried "$name" = "" after @Jack suggested, but still din't work .

Tilak Madichetti
  • 4,110
  • 5
  • 34
  • 52
  • http://shellcheck.net/ will catch this, and things like it, automatically. – Charles Duffy Nov 19 '16 at 15:39
  • This question is in no way specific to `if`. `[$name -eq ""]` would behave the same way if it were *not* inside an `if` condition, and likewise, the correct form -- `[ "$name" = "" ]`, or `test "$name" = ""`, or `[ -z "$name" ]`, or `test -z "$name"`, or `! test -n "$name"` -- will work just as well either inside or outside of `if`. – Charles Duffy Nov 19 '16 at 15:43
  • BTW, "still din't work" is not the same thing as "same error again". If you get a different error (and you certainly *would* have gotten a different error from Jack's suggestion, even misunderstanding it to no longer include `[ ]`; `Franco: command not found` is not the same error as `[Franco: command not found`), post that actual new error. – Charles Duffy Nov 19 '16 at 15:46
  • To try to explain the issue in yet another way: In `[ "$name" = "" ]` or `test "$name" = ""`, the `[` or `test` (literally, `/bin/[` and `/bin/test` are often two hardlinks to the same binary, or for shell-builtin implementations two hash table entries for the same function call); but regardless, it's a command named `[` or `test` that's running the comparison for you; it's not part of `if` syntax. You can't run that command unless you name it, and a command name isn't recognized as a command name if it's smushed against anything else without spaces. – Charles Duffy Nov 19 '16 at 15:50
  • ...so, just as how `ls/tmp` won't work but `ls /tmp` does, you need to run `[ "$foo" ]`, not `[$foo]`, so that the command name -- `[` -- is a separate word. (`]` is a mandatory closing argument to `[` for readability purposes, but inasmuch as that decision confuses people to think that `[ ]` is syntax instead of a regular command, it arguably was a bad one). – Charles Duffy Nov 19 '16 at 15:50
  • Hey Charles, I tell you what -z $name works perfect among the other options that your told – Tilak Madichetti Nov 20 '16 at 03:03

1 Answers1

0

Put a space between the square braces of your if condition and its contents.

if [ "$name" = "" ]; then

Additionally note that I use = over -eq to compare strings. -eq is used to compare integer values, while = will compare strings, which can be unintuitive coming from other languages.

I also quoted $name to prevent globbing and word splitting.

Jack Bracken
  • 1,233
  • 12
  • 23
  • Doesn't work Jack, its givin me another error- integer expression expected :) – Tilak Madichetti Nov 18 '16 at 11:33
  • -eq is for integer comparisons, to check string equality simply use $name = "". I'll edit. – Jack Bracken Nov 18 '16 at 11:35
  • 3
    Also note that he quoted `$name`. This is important in some shells when `$name` is empty, and it's just good practice. – LinuxDisciple Nov 18 '16 at 11:45
  • Oh no, its giving me the same error again . I had accepeted the answer last time w/o checking cuz i was on the mobile app :) Sorry – Tilak Madichetti Nov 19 '16 at 14:47
  • @Tilak, `[ "$name" = "" ]`, or `test "$name" = ""` -- the `[` or `test` (literally, `/bin/[` and `/bin/test` are often two hardlinks to the same binary, or for shell-builtin implementations the same function call), but regardless, it's a command named `[` or `test` that's running the comparison for you; it's not part of `if` syntax. – Charles Duffy Nov 19 '16 at 15:42