Run shell script on windows using Cygwin

4

2

I am having trouble with running .sh script on Windows using Cygwin.

When i run the file: ./build.sh -s srv.txt I am getting: ./build.sh: line 1: #!/bin/bash: No such file or directory

Can you guys help me what does that mean. The 1st line is commented :)

thanks.

elbek

Posted 2012-10-05T21:27:09.887

Reputation: 143

Is bash in /bin? Is it executable (chmod +x /bin/bash)? – zero2cx – 2012-10-05T21:46:01.720

I did it, yeah which bash is giving /usr/bin/bash – elbek – 2012-10-05T21:46:54.230

Answers

4

I had the same issue running cygwin under windows. For me, it had nothing to do with the path.

It had everything to do with the encoding. I was using UTF-8 with the BOM (Byte Order Mark). The BOM was causing it to choke. Once I removed it, it worked flawlessly.

I updated the encoding using Notepad++ by:

Encoding -> Encode in UTF-8 without BOM

The which bash command outputted:

/usr/bin/bash

However it was still able to resolve

#!/bin/bash

Ryan Gates

Posted 2012-10-05T21:27:09.887

Reputation: 662

4

It usually means there's a ^M (carriage return) at the end of each line of your file or some other encoding issue which prevents the #!/bin/bash line being interpreted correctly.

Take a copy of the file and save it as plain ASCII using an editor of your choice to remove as much non-ASCII encoding as possible. Also, try to save it in 'UNIX' format if the editor supports it.

EightBitTony

Posted 2012-10-05T21:27:09.887

Reputation: 3 741

What is vim? I have notepad++, I am on windows. – elbek – 2012-10-05T21:56:34.843

vim is a unix file editor, if the file won't run under Cygwin, the best bet is find out why under Cygwin. The problem is you're saving a windows file (which has carriage returns and linefeeds) and trying to run it under Cygwin. If you can, tell Notepad++ to save in 'unix' format. – EightBitTony – 2012-10-05T21:57:54.663

I did it, There is an option to make it UNIX format and I did it using notepad++ – elbek – 2012-10-05T21:59:41.983

Can you at least try what I suggest? Edit it in vim (in your Cygwin shell type, vim build.sh), and then type what I said, and see if the ^M symbols show up at the end of each line. – EightBitTony – 2012-10-05T22:00:28.357

Should I install vim for this? Getting: -bash: vim: command not found – elbek – 2012-10-05T22:01:49.050

It's part Cygwin, but I guess you don't have it installed. – EightBitTony – 2012-10-05T22:02:16.793

let us continue this discussion in chat

– EightBitTony – 2012-10-05T22:04:47.337

Installed vim, I opened the file with vim, I do not see any ^M. But Cygwin says vim is not found – elbek – 2012-10-05T22:05:17.250

Chat doesn't seem to be logging me in, what does this command (in cygwin) return, file build.sh – EightBitTony – 2012-10-05T22:06:30.220

Getting : build.sh: Bourne-Again shell script, UTF-8 Unicode (with BOM) text executable – elbek – 2012-10-05T22:07:37.670

The BOM is probably the issue. UTF-8 doesn't need a BOM anyway. See if you can save it without the BOM in your editor (maybe it offers a plain ASCII encoding), or else use a different editor. – jjlin – 2012-10-05T22:40:03.137

1Use this to see if you have the proper line endings: <pre>od -c</pre> This command shows exactly what characters are present. – UtahJarhead – 2012-10-06T01:03:32.130

Since you have Cygwin, you can also convert the line endings with the dos2unix utility. – Rich Homolka – 2013-06-18T14:18:46.730

3

Have you tried to run...

sh build.sh

Instead of...

./build.sh

I'm not sure (I never ran Cygwin) but it seems that /bin/bash is not a path in your machine, so your shebang (#!/bin/bash) blows the program.

If you run the program using sh the shebang should be treated as a comment.

Hope this helps.

Fábio Santos

Posted 2012-10-05T21:27:09.887

Reputation: 131

1

#!/bin/bash (called a shebang, short for Crash (#) Bang (!), tells the shell which program to use to execute the script in question. You'll see this with perl scripts (#!/usr/bin/perl), Python (#!/usr/bin/python), or php (#!/usr/bin/php) as well.

In your situation, a few things could be causing it. Either the 'bash' shell is not installed or the script is in an unrecognized format that you can't see.

First, does bash exist? Give us the results of this: ls -l /bin/bash

Second, what is in the script? Give us the first 2 lines of this: od -c build.sh What we're looking for is the script being in the wrong format. Windows likes CRLF'or \r\n for a line ending while Linux only likes LF or \n (see also: http://en.wikipedia.org/wiki/Newline)

To be honest, I don't know if CYGWIN likes Windows line endings or Linux, but that's a very likely cause for your issue.

Again, give us the output of od -c build.sh and we'll see what's up.

UtahJarhead

Posted 2012-10-05T21:27:09.887

Reputation: 1 755

0

In the past I have had /M problems in python scripts written on a windows box and trying to use them on a linux box. I used dos2unix to convert the files to make them operable on the linux box. If carriage returns (or other windows text formatting issues) exist and are the cause of the problem this might help.

CodeBandit

Posted 2012-10-05T21:27:09.887

Reputation: 1