【问题标题】:Why would a bash script produce the error "command not founde"?为什么 bash 脚本会产生错误“找不到命令”?
【发布时间】:2013-05-10 22:57:49
【问题描述】:

此脚本中的第 5 行有什么问题(我已经包含了给出错误的 sn-p,实际错误列在代码和完整脚本链接之后的底部)?

#! /bin/bash
INSTALLDIR=/usr/local/mapguideopensource
CLEAN_FLAG=0
while [ $# -gt 0 ]; do    # Until you run out of parameters...
  case "$1" in
    -prefix|--prefix)
        INSTALLDIR="$2"
        shift
        ;;
    -clean|--clean)
        CLEAN_FLAG=1
        shift
        ;;
    -help|--help)
        echo "Usage: $0 (options)"
        echo "Options:"
        echo "  --prefix [installation directory]"
        echo "  --clean [clean all objects and binaries in Oem]"
        echo "  --help [Display usage]"
        exit
        ;;
esac
shift   # Check next set of parameters.
done

这是我在 linux (REHL5) 上运行此 bash 脚本时遇到的错误:

: command not founde 4:  
: command not founde 8:  
: command not founde 8:  
: command not founde 12:  
MapGuide Open Source build script for OEM components  
'/build_oem.sh: line 17: syntax error near unexpected token `in  
'/build_oem.sh: line 17: `    case "$1" in  

请注意,上面的行号对应于我正在运行的实际脚本(我在下面包含了指向该脚本的链接) The original script i am running

【问题讨论】:

  • 任何“这个脚本有什么问题”的问题在定义上都过于本地化——它只与那个单一的脚本相关。

标签: bash shell


【解决方案1】:

从错误中,我很确定您在行尾有回车符(又名 CR 或 ^M)。 Windows/DOS 文本文件在每行的末尾都有回车和换行,但 unix 程序(如 bash)只需要换行,如果还有 CR,就会感到非常困惑。赠品是错误消息,例如:

: command not founde 4:

这实际上是./build_oem.sh: line 4: ^M: command not found,但是回车使终端回到行首,并将消息的结尾写在消息的开头:

./build_oem.sh: line 4: 
: command not found
       |
       V
: command not founde 4:

要修复脚本,请使用 dos2unix 将其转换为正确的 unix 格式,然后切换到以 unix 格式保存的文本编辑器。

【讨论】:

  • 谢谢。 dos2unix 为我工作(我还在被调用的子目录中的其他脚本上递归地运行它)。
【解决方案2】:

什么 choroba 说的,但还要注意你的 shebang 必须在第一行(它不是),否则它是无用的,因为它只是一个简单的评论,它不一定会在 @987654322 下执行@。

【讨论】:

    【解决方案3】:

    在原始脚本中,第 4 行和第 8 行是空的。行上可能有一些不可见的控制字符。试试xxd build_oem.sh

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2016-02-19
      • 1970-01-01
      • 2022-01-16
      • 2015-08-01
      相关资源
      最近更新 更多