cygwin bash - if statement doesn't work

0

I'm new to bash scripting, and I'm trying to learn on Windows, so I downloaded cygwin. Using Windows 10, cygwin v2.5.1

I made sure I installed the bash package (I did), and so I started going through some tutorials, but when I reached if...fi statements, it stopped working. Here's my code:

#!/usr/bin/env bash
#
echo 'Hello'
#
if [ 1 -eq 1 ]
then
  echo 'Success'
fi
echo 'Hello Again'

The output is

>./test.sh
Hello
./test.sh: line 10: syntax error: unexpected end of file

I can't for the life of me figure out what's going wrong. I've copy/pasted if statements and tried different types of expressions, but it doesn't seem to register the if at all.

Maybe I configured cygwin wrong?

Cappielung

Posted 2016-06-21T22:44:12.700

Reputation: 1

Missing semi-colon maybe? Try "if [ ... ]; then". Note the semi-colon after the square brackets. – jehad – 2016-06-21T22:49:46.770

Also, I know the multi-statement semi-colon is just stylistic, but maybe something in cygwin version needs it!? Don't know, not really a Windows guy. :) – jehad – 2016-06-21T22:53:47.830

Answers

0

Well...I feel dumb. But I'll answer this question for anyone else having the same problem.

Since I'm developing on a Windows computer, it's using CRLF line endings. I use Sublime Text and never think about line endings because I'm always developing for Windows.

But, of course, bash scripts need to use LF line endings, or else weird stuff happens. I couldn't say why it lets echo commands execute and not if...fi, but when I switched my line endings to LF, everything worked as you'd expect.

Cappielung

Posted 2016-06-21T22:44:12.700

Reputation: 1

The script looked OK, and on copying and pasting into Ubuntu it ran fine in bash. I've not noticed a problem with CR/LF new-lines before, as the superfluous CRs should be treated as white space, but when I added the CRs to your pasted script I got the same error. Yet, when I commented out the if, then and fi lines, all the echo commands worked as expected. You've raised an interesting problem, and I shall investigate further, but not tonight. – AFH – 2016-06-21T23:33:14.513

1I think I've got to the bottom of it: CR apparently is not treated as white space, but as a text character. Appending CR to the # lines does not affect their being comment lines, while the echo lines with trailing CRs are still valid syntax, taking the trailing CRs as part of the text to output. The if and [ are correctly parsed, but ], then and fi are not, because of the appended CR, and the end of file is encountered before the completion of the if syntax is complete. – AFH – 2016-06-22T16:00:22.373

0

It works for me, but if I change the line termination to DOS style (CR/LF line termination)

$ u2d ./prova.sh
unix2dos: converting file ./prova.sh to DOS format...

$ ./prova.sh
Hello
./prova.sh: line 10: syntax error: unexpected end of file

so convert your script to unix style (LF as line end)

$ d2u ./prova.sh
dos2unix: converting file ./prova.sh to Unix format...
$ ./prova.sh
Hello
Success
Hello Again

matzeri

Posted 2016-06-21T22:44:12.700

Reputation: 1 662