14

I have two similar scripts with different names. One works fine but other throws error. Can anyone please tell me what is the issue?

This is my test.sh scripts which works fine

[nnice@myhost Scripts]$ cat test.sh 
#!/bin/bash
function fun {     
        echo "`hostname`"
}
fun 
[nnice@myhost Scripts]$ ./test.sh 
myhost.fedora

Here is my another script demo.sh but it throws error

[nnice@myhost Scripts]$ cat demo.sh 
#!/bin/bash
function fun { 
    echo "`hostname`"
}
fun
[nnice@myhost Scripts]$ ./demo.sh 
bash: ./demo.sh: cannot execute: required file not found

Both scripts having the same permissions

[nnice@myhost Scripts]$ ll test.sh 
-rwxr-xr-x. 1 nnice nnice 65 Oct 21 10:47 test.sh
[nnice@myhost Scripts]$ ll demo.sh 
-rwxr-xr-x. 1 nnice nnice 58 Oct 21 10:46 demo.sh
4
  • Note: use of back ticks e.g. ... `command` ... is deprecated. Use the newer easier to use "$(command)" (but yes this simple use with echo is hoop jumping). Use of .sh at the end of file names is not Unix. And it violates the principle of abstraction, as it leaks implementation detail. Just name them by what they do, not how they do it. Commented Oct 21, 2022 at 8:16
  • @ctrl-alt-delor They are most definitely not deprecated, only awkward to use and should be avoided. In this particular case, any type of command substitution would be inappropriate. As for naming files, that's really up to the user. If you are concerned with abstraction, you could suggest the user uses the hostname command directly rather than via a nonsensical script with a function.
    – Kusalananda
    Commented Oct 21, 2022 at 8:22
  • 1
    deprecate : express disapproval of. I disapprove. You say should be avoided. Thus deprecated. As for up to the user. I agree, it is the same for drug use. And the best abstraction is no abstraction (it depends). However I assume the examples were minimum non-working examples. Commented Oct 21, 2022 at 8:29
  • 1
    fwiw, this error is usually a bad shebang, see also stackoverflow.com/a/18818809/127971
    – michael
    Commented Jan 7, 2023 at 3:48

6 Answers 6

35

Your demo.sh script is a DOS text file. Such files have CRLF line endings, and that extra CR (carriage-return) character at the end of the line is causing you issues.

The specific issue it's causing is that the interpreter pathname on the #!-line now refers to something called /bin/bash\r (with the \r symbolising a carriage-return, which is a space-like character, so it's usually not visible). This file is not found, so this is what causes your error message.

To solve this, convert your script from a DOS text file to a Unix text file. If you are editing scripts on Windows, you can probably do this by configuring the Windows text editor to create Unix text files, but you may also use the dos2unix utility, available for most common Unix variants.

$ ./script
bash: ./script: cannot execute: required file not found
$ dos2unix script
$ ./script
harpo.local

Regarding your code: Please never do echo `some-command` or echo $(some-command) to output the output of some-command. Just use the command directly:

#!/bin/sh

fun () {
    hostname
}

fun

(Since the script now does not use anything requiring bash, I also shifted to calling the simpler /bin/sh shell.)

0
7

(Because my search ended up here), I had test.sh

#!/bin/bash
echo "test"

that I copied to NixOS and got

-bash: ./test.sh: cannot execute: required file not found

I tried my default vim method of removing windows carriage-returns:

:e ++ff=unix 
:%s/\r\(\n\)/\1/g

but that didn't help.

I changed the first line to

#!/usr/bin/env bash

and it now works.

I could have changed it to

#!/bin/sh

as I wasn't using any bashism but in another script I was so I needed a nice portable way to invoke bash.

2

If you open the file in notepad++ you can see carriage-return (CL RF) bottom right corner of the file, you can double click and change to "Unix (LF)".

refer below image

0

Docker Dev Containers

This seems to occur if you clone a Git repository to a Windows file system, and then (using VS Code) open that directory within a Docker Dev Container

In other words you might encounter this if you did the following things:

  • Installed Git for Windows with all default options
  • Cloned a repo to Windows file system
  • Opened this directory with VS Code
  • Remote Connection (bottom left) -> Reopen in Dev Container

Inside the dev container, git diff YOUR_BASH_SCRIPT.sh does not show any obvious differences.

It will show that every line has been changed. It will show all lines deleted and then all lines added.

Quite why this happens I am not sure. It does not seem to matter if you wrote your bash script from another Linux system first. It is the act of cloning a repo to a Windows FS which breaks everything.

The only solution I know of right now is to re-write your bash script inside the container.

It may also work if you clone the repository within the Dev Container. But that might not exactly be easy/convenient, since you need to create and open a Dev Container first, then clone, rather than cloning to Windows first, then opening the Dev Container.

Sorry I know this isn't much of an answer (at least not right now, I hope to update it in future as I obtain more information) - if you can contribute something please leave a comment.

0

For those who have the same issue with Docker Dev Container mentioned by @user3728501, you can try to remove the carriage returns (CR, "\r", 0x0d) from your script file before running it. This post shows different ways of doing so.

I was able to resolve this issue by adding the below in my devcontainer.json.

"postCreateCommand": sed -i 's/\r$//' your-script.sh; your-script.sh
-1

In my instance, on Windows, this was caused by the bash script I was trying to execute using this shebang #! /user/bin/bash instead of #!/bin/bash.

You must log in to answer this question.

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