Bash 脚本:错误的解释器

新手上路,请多包涵

问题:我收到此错误消息:

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

当我执行这个 bash 脚本时:

 #!/bin/bash
MONO_PREFIX=/opt/mono-2.6
GNOME_PREFIX=/opt/gnome-2.6
export DYLD_LIBRARY_PATH=$MONO_PREFIX/lib:$DYLD_LIBRARY_PATH
export LD_LIBRARY_PATH=$MONO_PREFIX/lib:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=$MONO_PREFIX/include:$GNOME_PREFIX/include
export ACLOCAL_PATH=$MONO_PREFIX/share/aclocal
export PKG_CONFIG_PATH=$MONO_PREFIX/lib/pkgconfig:$GNOME_PREFIX/lib/pkgconfig
PATH=$MONO_PREFIX/bin:$PATH
PS1="[mono-2.6] \w @ "

但是 bash 路径似乎是正确的:

 asshat@IS1300:~/sources/mono-2.6# which bash
/bin/bash

asshat@IS1300:~# cd sources/
asshat@IS1300:~/sources# cd mono-2.6/
asshat@IS1300:~/sources/mono-2.6# ./mono-2.6-environment
export: bad interpreter: No such file or directory
asshat@IS1300:~/sources/mono-2.6# ls
download  mono-2.4  mono-2.4-environment  mono-2.6  mono-2.6-environment
asshat@IS1300:~/sources/mono-2.6# cp mono-2.6-environment mono-2.6-environment.sh
asshat@IS1300:~/sources/mono-2.6# ./mono-2.6-environment.sh
export: bad interpreter: No such file or directory
asshat@IS1300:~/sources/mono-2.6# ls
download  mono-2.4-environment  mono-2.6-environment
mono-2.4  mono-2.6              mono-2.6-environment.sh
asshat@IS1300:~/sources/mono-2.6# bash mono-2.6-environment
asshat@IS1300:~/sources/mono-2.6#

我究竟做错了什么?或者这是一个 Lucid Lynx 错误?

我做了 chmod + x

原文由 Stefan Steiger 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 747
2 个回答

第一行 #!/bin/bash 告诉 Linux 在哪里可以找到解释器。该脚本也应该可以使用 chmod +x script.sh 来执行,您似乎已经这样做了。

您很可能使用 Windows 编辑器创建了此文件,该编辑器将在每一行的末尾放置一个 <cr><lf> 。这是dos/windows下的标准。 OS X 将在每一行的末尾放置一个 <cr> 。但是,在 Unix/Linux 下,标准是在行尾放一个 <lf>

Linux现在正在寻找一个名为 /bin/bash<cr> 的文件来解释该文件,其中 <cr> 是一个回车符,在Linux下是一个有效的文件字符。这样的文件不存在。因此错误。

解决方案: 在 Linux 上用编辑器编辑文件,去掉多余的 <cr> 。在 Windows 上编辑文件时通常可以使用的一种工具是 dos2unix

原文由 Matthias Wandel 发布,翻译遵循 CC BY-SA 4.0 许可协议

脚本可以使用 Dos 换行符吗?

尝试在其上运行 dos2unix。

原文由 Anton 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题