运行 Bash 脚本会导致“错误的解释器:没有这样的文件或目录”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41918133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:40:31  来源:igfitidea点击:

Running a Bash script results in 'Bad interpreter: No such file or directory' error

linuxbashshellfile-permissions

提问by john400

I did found questions on

我确实发现了问题

Bad interpreter: No such file or directory thing on SO.

错误的解释器: SO 上没有这样的文件或目录

My issue is also solved when I changed the script from

当我更改脚本时,我的问题也解决了

#!/usr/bin/bash
echo -e "\t\t\e[92mHello from the Test Script!\e[39m"

to:

到:

#!/bin/bash
echo -e "\t\t\e[92mHello from the Test Script!\e[39m"

after I did the first line change from looking an answer here.

在我从这里寻找答案做了第一行更改之后。

Shell script: Bad interpreter.No such file or directory

Shell 脚本:错误的解释器。没有那个文件或目录

I can not understand why removing the /usr from the first line helps.

我不明白为什么从第一行删除 /usr 有帮助。

P.S.I am learning about linux file permissions and I was unable to execute my file even after changing the permission using '755'. So, please if anyone can explain me this.Thanks in advance.:)

PS我正在学习 linux 文件权限,即使在使用“755”更改权限后我也无法执行我的文件。所以,请如果有人能解释我这一点。提前致谢。:)

回答by Burhan Khalid

On your system, the bashshell lives in /bin/bashand not /usr/bin/bash.

在您的系统上,bashshell 位于/bin/bash而不是/usr/bin/bash.

The path after the !should be the path to an executable that will be passed the contents of the script as an argument.

后面的路径!应该是可执行文件的路径,该可执行文件将作为参数传递脚本的内容。

You can read more about this at wikipedia

你可以在维基百科上阅读更多关于这个的信息

As for the second part of your question; it would not have mattered what the permissions are; as the file was pointing to a bad interpreter.

至于你问题的第二部分;权限是什么并不重要;因为文件指向一个错误的解释器。

For more on unix file permissions, I suggest reading this entry on wikipedia.

有关 unix 文件权限的更多信息,我建议阅读wikipedia 上的此条目

回答by SkorpEN

In my case adding shbefore script name solved the issue.

在我的情况下,sh在脚本名称之前添加解决了这个问题。

回答by codeforester

That's because there is no bash binary at /usr/bin/bashand the correct path for bash is /bin/bash.

那是因为没有 bash 二进制文件,/usr/bin/bash而bash的正确路径是/bin/bash.

The #!line at the top of scripts, called the shebang, determines what program (sh, bash, ruby, perl, python, etc.) is used for running the script.

#!脚本顶部的行,称为shebang,确定用于运行脚本的程序(sh、bash、ruby、perl、python 等)。

This post covers this topic well:

这篇文章很好地涵盖了这个主题:

https://unix.stackexchange.com/questions/87560/does-the-shebang-determine-the-shell-which-runs-the-script

https://unix.stackexchange.com/questions/87560/does-the-shebang-determine-the-shell-which-runs-the-script

回答by HelpBox

You can also call your script by adding "./" at the beginning in case you call it from the local directory. The other solution is to call it by specifying its full path.

您还可以通过在开头添加“./”来调用脚本,以防您从本地目录调用它。另一种解决方案是通过指定其完整路径来调用它。