5
#!/bin/bash
if test "$#" == "4"; then echo "$*"; else echo "args-error" >&2; fi;

This little code snippet troubles me a lot when I tried to run it on both Ubuntu and Cygwin.

Ubuntu runs bash version 4.0+ whereas Cygwin runs 3.2.49; But I reckon version collision shall not be the cause of this, this code runs well under fedora 10 which is also using bash version 3.+

So basically I am wondering if there is a way to code my script once and for all so there are not to have this awful issue later on.

Many thanks in advance.

Edited : I don't have Cygwin by hand at the moment but from my memory, it keeps saying something like couldn't resolve undefined token "fi" something like that.

Edited : well,the original form is like this, just found from server :

#!/bin/bash
if ["$#" == "4"];
    then echo "$*";
    else echo "args-error" >&2;
fi;

Console complains :

$ ./test.sh 1 2 3
./test.sh: line 2: [3: command not found
args-error

I am also wondering how come that stderr says something goes wrong - command not found - but can still print out the answer?

3
  • 1
    What is the error you are getting? This code snippet seems to work just fine for me (Ubuntu 9.10, bash 4.0.33). Commented Mar 25, 2010 at 22:56
  • 1
    Looks fine on my Cygwin (also bash 3.2.49). What is the error message? Commented Mar 25, 2010 at 22:58
  • @mobrule : sorry, that is the fixed version by using test statement, I've updated the question, now there is the error... Commented Mar 25, 2010 at 23:05

4 Answers 4

7

You need whitespace around the [ and ].

#!/bin/bash
if [ "$#" == "4" ];
    then echo "$*";
    else echo "args-error" >&2;
fi;
Sign up to request clarification or add additional context in comments.

5 Comments

@mobrule : thanks! Ahhh, never thought something like this can make the error ... get used to Java syntax ...
It's because [ is actually an alias for what used to be a separate program in Old Unix, called test. You can write the above as "if test "$#" == "4" ]... Now it should make sense why the spaces are needed.
@WarrenYoung, one correction -- old-style unix test (and, indeed, the POSIX standard) doesn't allow == as an operator; it needs to be either = for string comparison or -eq for numeric comparison.
@CharlesDuffy: Your comment is misplaced. The OP asked the question with ==, and all of the answers use == to reduce confusion. You should be commenting on the question, above.
@WarrenYoung, I commented here as you were specifically speaking as to compatibility with the traditional test command.
4

Updated answer:

You need a space after [ otherwise ["$#" is evaluated to for example [3 which doesn't exist. Try this:

if [ "$#" == "4" ];
    then echo "$*";
    else echo "args-error" >&2;
fi;

It works for me. I would guess that you are getting an error like this:

test.sh: line 2: $'\r': command not found
test.sh: line 3: $'\r': command not found

This can happen because you have edited the file using Windows-style line endings but Bash expects Unix-style line endings. To fix the file, try running this command:

dos2unix test.sh

You of course need to change the filename to the actual filename of your script.

1 Comment

@Mark Byers: still same error for the original version. Strange thing is, it prints out some stderr and then gets the answer to stdout. My understanding is if something goes wrong, the script execution should be killed...
2

In your edit to show the 'original form' the problem would seem to be that you're missing a space between the [ and the "

Comments

1

try to use operator '=' instead of '==' . And also add one space after [ and another before ] as folowing

if [ "$#" = "4" ];
then echo "$*";
else echo "args-error" >&2;
fi;

Or even try '-eq' instead of '=='

if [ "$#" -eq "4" ];

Because in some systems, it does not accept the operator '=='

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.