bash 尝试运行 shell 脚本时出现错误“:找不到命令”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25252573/
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 11:05:33  来源:igfitidea点击:

Getting error ": command not found" when trying to run shell script

bashshellunix

提问by Erik

I have a script that I'm trying to run but I just get the error ": command not found" whenever I try to run it. Here's what I've tried to do to fix it:

我有一个我正在尝试运行的脚本,但是每当我尝试运行它时,我都会收到错误“:找不到命令”。这是我试图修复它的方法:

  1. Made sure the hashbang is correct "#!/bin/bash"
  2. Run dos2unix on the file
  3. Run the script as scriptname.sh, ./scriptname.sh, and /bin/bash scriptname.sh
  4. chmod 755 scriptname.sh
  1. 确保 hashbang 是正确的“#!/bin/bash”
  2. 在文件上运行 dos2unix
  3. 将脚本作为 scriptname.sh、./scriptname.sh 和 /bin/bash scriptname.sh 运行
  4. chmod 755 脚本名.sh

I still am unable to run the script. Any help is much appreciated!

我仍然无法运行脚本。任何帮助深表感谢!

回答by that other guy

This is caused by carriage returns. Here's the excerpt from the bash tag wiki:

这是由回车引起的。这是bash 标签 wiki的摘录:

  1. Check whether your script or data has DOS style end-of-line characters

    • Use cat -v yourfileor echo "$yourvariable" | cat -v.

      DOS carriage returns will show up as ^Mafter each line.

      If you find them, delete them using dos2unix(a.k.a. fromdos) or tr -d '\r'

  1. 检查你的脚本或数据是否有 DOS 风格的行尾字符

    • 使用cat -v yourfileecho "$yourvariable" | cat -v

      DOS 回车将显示^M在每一行之后。

      如果找到它们,请使用dos2unix(aka fromdos) 或tr -d '\r'

Make sure to check all your data, and not just the script itself.

确保检查所有数据,而不仅仅是脚本本身。

回答by konsolebox

You can use these to delete unnecessary characters:

您可以使用这些来删除不需要的字符:

tr -cd '[:alnum:][:blank:][:punct:]\n' < script.sh > new_script.sh

Or

或者

tr -cd '[:graph:][:blank:]\n' < script.sh > new_script.sh

Then try new_script.sh.

然后尝试new_script.sh