#!/bin/bash - 没有这样的文件或目录

Nic*_*nay 94 bash executable shell-script shebang

我已经创建了一个 bash 脚本,但是当我尝试执行它时,我得到

#!/bin/bash no such file or directory
Run Code Online (Sandbox Code Playgroud)

我需要运行命令:bash script.sh让它工作。

我怎样才能解决这个问题?

jll*_*gre 125

这种消息通常是由于虚假的shebang行,第一行末尾的额外回车或开头的BOM。

跑:

$ head -1 yourscript | od -c
Run Code Online (Sandbox Code Playgroud)

看看它是如何结束的。

这是错误的:

0000000   #   !   /   b   i   n   /   b   a   s   h  \r  \n
Run Code Online (Sandbox Code Playgroud)

这也是错误的:

0000000 357 273 277   #   !   /   b   i   n   /   b   a   s   h  \n
Run Code Online (Sandbox Code Playgroud)

这是对的:

0000000   #   !   /   b   i   n   /   b   a   s   h  \n
Run Code Online (Sandbox Code Playgroud)

如果这是问题,请使用dos2unix(或sed, tr, awk, perl, python...) 修复您的脚本。

这是一个将同时删除 BOM 和尾随 CR 的方法:

sed -i '1s/^.*#//;s/\r$//' brokenScript
Run Code Online (Sandbox Code Playgroud)


请注意,您用来运行脚本的 shell 会稍微影响显示的错误消息。

以下是三个脚本,仅显示它们的名称 ( echo $0) 并具有以下各自的 shebang 行:

正确的脚本:

0000000   #   !   /   b   i   n   /   b   a   s   h  \n
Run Code Online (Sandbox Code Playgroud)

scriptWithBom:

0000000 357 273 277   #   !   /   b   i   n   /   b   a   s   h  \n
Run Code Online (Sandbox Code Playgroud)

带CRLF的脚本:

0000000   #   !   /   b   i   n   /   b   a   s   h  \r  \n
Run Code Online (Sandbox Code Playgroud)

在 bash 下,运行它们将显示以下消息:

$ ./correctScript
./correctScript
$ ./scriptWithCRLF
bash: ./scriptWithCRLF: /bin/bash^M: bad interpreter: No such file or directory
$ ./scriptWithBom
./scriptWithBom: line 1: #!/bin/bash: No such file or directory
./scriptWithBom
Run Code Online (Sandbox Code Playgroud)

通过显式调用解释器来运行伪造的脚本可以让 CRLF 脚本毫无问题地运行:

$ bash ./scriptWithCRLF
./scriptWithCRLF
$ bash ./scriptWithBom
./scriptWithBom: line 1: #!/bin/bash: No such file or directory
./scriptWithBom
Run Code Online (Sandbox Code Playgroud)

这是在 下观察到的行为ksh

$ ./scriptWithCRLF
ksh: ./scriptWithCRLF: not found [No such file or directory]
$ ./scriptWithBom
./scriptWithBom[1]: #!/bin/bash: not found [No such file or directory]
./scriptWithBom
Run Code Online (Sandbox Code Playgroud)

和下dash

$ ./scriptWithCRLF
dash: 2: ./scriptWithCRLF: not found
$ ./scriptWithBom
./scriptWithBom: 1: ./scriptWithBom: #!/bin/bash: not found
./scriptWithBom
Run Code Online (Sandbox Code Playgroud)

  • `dos2unix` 还删除了一个 UTF-8 BOM。UTF-8 BOM 可以解释错误消息。 (6认同)
  • 揭示这是否是问题的另一种方法是`hexdump -C yourscript | 头-n 1`。我仍然会使用 `dos2unix yourscript` 来修复它。 (3认同)
  • 如果是 CRLF 问题,您将不会看到 `#!/bin/bash no such file or directory` 错误消息,因为没有任何理由尝试执行或打开 `#!/bin/bash`。将执行的是`/bin/bash<CR>`。 (2认同)

tek*_*aul 19

这也可能是由 UTF-8 脚本中的 BOM 引起的。如果您在 Windows 中创建脚本,有时您会在文件开头看到一些垃圾。


moe*_*eye 12

实际上,bash 脚本的正确shebang 是这样的:

#!/usr/bin/env bash
Run Code Online (Sandbox Code Playgroud)

因为,在 freeBSD 中,bash 位于 /usr/local/bin/bash

  • 在这种情况下,“正确”是一个很难使用的词。也许更好的说法是“不易出错”。 (14认同)
  • 这也太可怕了;/usr 存在的假设是一个糟糕的 IMO。例如,Haiku 没有 /usr。 (3认同)

cwa*_*ash 11

如果存在这两个问题,您可以使用 vi 来解决它们:

vi <your_file>
:set ff=unix
:set nobomb
:wq
Run Code Online (Sandbox Code Playgroud)

  • @G-Man 其他答案已经比我希望的更详细地提到了这一点。无需重复,但如果不是很明显,您可以使用 WINdows 行结尾和隐藏的 Windows BOM 字符。我认为很多浏览答案的人都喜欢简洁而不是自给自足,尤其是当其他答案中有更多细节时。 (2认同)