Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cygwin bash syntax error - but script run perfectly well in Ubuntu

Tags:

bash

shell

cygwin

#!/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?

like image 1000
Michael Mao Avatar asked Mar 25 '10 22:03

Michael Mao


People also ask

Can CygWin run bash scripts?

into a CygWin terminal type the command ls /bin/bash.exe : it list the executable for bash. open a windows CMD and type the command dir C:\cygwin\bin\bash.exe : it list the executable for bash.

What is bash CygWin?

description: Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003. 2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use.

How fix Unexpected end of file in Linux?

An Unexpected end of file error in a Bash script usually occurs when you there is a mismatched structure somewhere in the script. If you forget to close your quotes, or you forget to terminate an if statement, while loop, etc, then you will run into the error when you try to execute your Bash script.

How do you end a file in bash?

There are many methods to quit the bash script, i.e., quit while writing a bash script, while execution, or at run time. One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.


1 Answers

You need whitespace around the [ and ].

#!/bin/bash
if [ "$#" == "4" ];
    then echo "$*";
    else echo "args-error" >&2;
fi;
like image 149
mob Avatar answered Sep 28 '22 13:09

mob