sudo: unable to execute ./script.sh: no such file or directory
This usually happens when the shebang (#!
) line in your script is broken.
The shebang is what tells the kernel the file needs to be executed using an interpreter. When run without sudo
, the message is a little more meaningful. But with sudo
you get the message you got.
For example:
$ cat test.sh
#!/bin/foo
echo bar
$ ./test.sh
bash: ./test.sh: /bin/foo: bad interpreter: No such file or directory
$ bash test.sh
bar
$ sudo ./test.sh
sudo: unable to execute ./test.sh: No such file or directory
$ sudo bash ./test.sh
bar
The bad interpreter
message clearly indicates that it's the shebang which is faulty.
I just had this exact problem, it turned out to be a text file encoding problem. For me to fix it while running Xubuntu 14.04.3 LTS, I installed dos2unix and converted the script's encoding and then ran the script again using sudo and it worked fine. You can find an example below:
sudo apt-get install dos2unix -y
dos2unix test.sh
sudo chmod u+x test.sh && sudo ./test.sh