21

I'm trying to execute a bash script (git-ftp) but I can't seem to do it. This is what happens:

[trusktr@rocketship ~]$ ~/scripts/git-ftp
: No such file or directory

The file has permissions 755.

This is the contents of the script: http://pastie.org/3567556

Why am I getting this error? What can I do to fix the problem?

2

3 Answers 3

39

I have seen this error if the script has windows line endings instead of unix line endings. Try running dos2unix on the script and see if you get the same error.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow, interesting. That was exactly it.
You mean that exact error with the colon: <empty>: no such file or directory. Makes sense; an empty command. It's not actually empty but the string "\r" The leading carriage return disappears when printed since on your Unix terminal it just moves the cursor to column 0.
@Kaz: It's more complicated than that. The shebang line in the script is #!/usr/bin/env bash\r, so env prints the error "env: bash\r: No such file or directory". The carriage return makes the second part (": No such file or directory") overwrite the first part, so you don't see the first part. (p.s.: well spotted, joesdiner.)
1

Is there a #! (hash bang) line in the script, and does the pathname resolve?

If the script is running, it may be something in the script. Add this command to the top of the script, before any other command (but of course after the hash bang, if there is one):

set -x   # enable trace mode

3 Comments

Yeah, if you look at the script, it has "#!/usr/bin/env bash" at the top. I tried adding set -x but nothing new happens.
Try bash -x ~/scripts/git-ftp and see if that reveals anything. FWIW the script runs fine for me printing the usage message
Thanks for trying to help me. Turns out @joesdiner was exactly right: I needed to convert all line endings to unix style.
0

As joesdiner and Gordon Davisson point out, the error is likely happening because the script starts with an env shebang that ends with a CR/LF line terminator, e.g. #!/usr/bin/env bash\r\n

When the script is executed, something like /usr/bin/env $'bash\r' ~/scripts/git-ftp is actually run. A program called bash\r (with a CR character at the end) is - hopefully! - unlikely to be found, and env will print:

env: bash\r: No such file or directory

But the \r will send the cursor back to the start of the line, and the env: bash text will be overwritten by what follows, i.e.:

: No such file or directory

Some versions of env will at least be decent enough to escape the literal \r:

env: ‘bash\r’: No such file or directory

But either way, it will first look for a bash\r executable in the path and try to execute it.

This is arguably a dangerous thing for env to do, since it raises the possibility of someone adding a bash\r executable somewhere in the user's path.

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.