4

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.

2
  • Is bash in /bin? Is it executable (chmod +x /bin/bash)?
    – zero2cx
    Oct 5, 2012 at 21:46
  • I did it, yeah which bash is giving /usr/bin/bash
    – elbek
    Oct 5, 2012 at 21:46

5 Answers 5

5

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
5

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.

13
  • What is vim? I have notepad++, I am on windows.
    – elbek
    Oct 5, 2012 at 21:56
  • 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. Oct 5, 2012 at 21:57
  • I did it, There is an option to make it UNIX format and I did it using notepad++
    – elbek
    Oct 5, 2012 at 21:59
  • 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. Oct 5, 2012 at 22:00
  • 1
    Use this to see if you have the proper line endings: <pre>od -c</pre> This command shows exactly what characters are present. Oct 6, 2012 at 1:03
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.

0
2

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

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .