2

I get an issue using a shell script on Ubuntu.

My script :

#!/bin/bash
/usr/local/bin/node ./index.js
exec bash

I have set it as executable with:

sudo chmod +x ./index.sh

Then run it with:

sudo ./index.sh

But get:

sudo: unable to execute ./index.sh: No such file or directory

However, the file is there:

$ ls -la 
-rwxr-xr-x   1 root root    54 oct.   4 10:05 index.sh
Stephen Kitt
  • 434,908
tonymx227
  • 201
  • 4
  • 14
  • Can you give us the result of a ls -al ? – Carpette Oct 04 '17 at 08:25
  • You don't need ./ when you are changing permissions and so sudo chmod +x index.sh. Also try running the command with the full path and so sudo /index.sh – Raman Sailopal Oct 04 '17 at 08:32
  • See edit with the command ls -la. And same result with the full path : sudo: unable to execute [FULLPATH]/index.sh: No such file or directory – tonymx227 Oct 04 '17 at 08:37
  • Where is your bash? Use type bash to find it, then update the #!-line in the script. For this simple script, though, you don't need bash: #!/bin/sh would be enough. – Kusalananda Oct 04 '17 at 08:39
  • My bash is here : /bin/bash. And I get the same message error after replacing #!/bin/bash to #!/bin/sh... – tonymx227 Oct 04 '17 at 08:43
  • Is sudo aliased to something? – Kusalananda Oct 04 '17 at 08:45
  • Try sudo $PWD/index.sh – jlliagre Oct 04 '17 at 08:46
  • What Unix is this running on? – Kusalananda Oct 04 '17 at 08:47
  • No I've never aliased sudo to something. The distribution is Ubuntu. – tonymx227 Oct 04 '17 at 08:47
  • sudo $PWD/index.sh : sudo: unable to execute /var/www/folder1/folder2/index.sh: No such file or directory – tonymx227 Oct 04 '17 at 08:49
  • 1
    Did you by any chance edit the file on Windows? In this case, then newlines may be DOS newlines. Use dos2unix to remove these, or tr -d '\r' <index.sh >index.sh-new to filter all \r out. – Kusalananda Oct 04 '17 at 08:52
  • 1
    Please show us the output of both sudo $PWD/index.sh and sudo ls -l $PWD/index.sh and also sudo ls $PWD/index.sh | od -c. But add them to your question. Copy the output from your terminal exactly as it is and use the formatting tools to format it as code. Also, what happens if you try typing sudo $PWD/index and then hitting TAB? Also, what is the actual path? Are there any spaces in the names of folder1 or folder2? – terdon Oct 04 '17 at 08:54
  • 3
    Ok so dos2unix seems to be the solution. It works. – tonymx227 Oct 04 '17 at 09:05
  • Duplicate of https://unix.stackexchange.com/questions/108588/shebang-line-not-working-with-cr-lf (and https://unix.stackexchange.com/questions/189254/no-such-file-or-directory-but-i-can-see-it and …) – Gilles 'SO- stop being evil' Oct 04 '17 at 14:35

1 Answers1

4

The script was at some point edited on a Windows machine. Editors on Windows usually use DOS line-endings. These have an extra carriage return (\r) compared to Unix line-endings. These confuse the kernel when sudo asks it to run the script.

To fix the script, run dos2unix over it.

Alternatively, remove all \r in the script with

$ tr -d '\r' <index.sh >index.sh-new
$ # test index.sh-new to make sure it works
$ mv index.sh-new index.sh

This will obviously break the script if it relies on handling literal carriage returns in some way.

Stephen Kitt
  • 434,908
Kusalananda
  • 333,661
  • 1
    And there I was poised with the noexec mount of /var/www answer. (-: – JdeBP Oct 04 '17 at 09:16
  • 1
    For added fun, the error message arises because sudo is involved. sudo calls one of the exec syscalls, which fails with ENOENT because the interpreter doesn’t exist (with a \r), and that’s that; sudo prints the corresponding “no such file or directory” message. Running the script directly from a shell would give a “bad interpreter” error instead, because when a shell sees an error from exec, it reads the script itself and attempts to handle it directly: in this case it sees the shebang and also fails to find the interpreter, resulting in the “bad interpreter” error. – Stephen Kitt Oct 04 '17 at 09:48
  • @StephenKitt This depends on the shell though. ksh will, for example, not say "bad interpreter", but zsh and bash will. – Kusalananda Oct 04 '17 at 09:52
  • @Kusalananda it does indeed, thanks for checking ;-). – Stephen Kitt Oct 04 '17 at 09:55