尝试在 Linux 上运行 shell 脚本时出现“无法执行二进制文件”

新手上路,请多包涵

我对 linux 和 shell 脚本很陌生。我正在尝试使用以下命令从 linux 上的安全 shell (ssh) 运行 shellscript:

 chmod +x path/to/mynewshell.sh

sh path/to/mynewshell.sh

我收到此错误:

 path/to/mynewshell.sh: path/to/mynewshell.sh: cannot execute binary file.

尝试使用此命令:

 bash path/to/mynewshell.sh

我犯了同样的错误。

尝试使用此命令: su - myusername sh path/to/mynewshell.sh 它正在询问我的密码并给我这个错误: no such file or directory

1. cat -v path/to/mynewshell.sh 的结果是:^@^@^@^@^@^@^@^@Rscript “$dir”/diver_script.R done

2.当尝试’less path/to/mynewshell.sh’时,我在终端上得到了这个:

 #!/bin/bash/Rscript^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
for dir in /path/to/* ; do
^@^@^@^@^@^@^@^@Rscript "$dir"/myRscript.R
done

3.当我运行文件 path/to/mynewshell.sh 时:我得到了这个“Bourne-Again shell script text executable”

请就如何尝试执行 shellscript 提供任何建议。

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

阅读 2.6k
2 个回答

chmod -x 从文件中删除执行权限。做这个:

 chmod +x path/to/mynewshell.sh

并运行它

/path/to/mynewshell.sh

正如错误报告所说,你的脚本实际上不是一个脚本,它是一个二进制文件。

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

我通过 PowerShell 中的 bash 解释器运行我的 shell 脚本时遇到了同样的错误。我在 shell 脚本上运行了 dos2unix myscript.sh ,现在它运行正常。

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

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