【问题标题】:syntax error near unexpected token `$'in\r'意外标记 `$'in\r' 附近的语法错误
【发布时间】:2018-08-10 12:38:27
【问题描述】:

当我构建 shell 脚本时出现以下错误,因为我是 CentOS 的新手,无法找出原因,谁能帮助我解决这个问题??

我猜我们需要在执行时传递命令行参数我不确定这里必须作为参数传递什么。

Shell 脚本:

#!/bin/sh

# Default place to look for apr source.  Can be overridden with
#   --with-apr=[directory]
apr_src_dir=../apr

while test $# -gt 0
do
  # Normalize
  case "$1" in
  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case "$1" in
  --with-apr=*)
  apr_src_dir=$optarg
  ;;
  esac

  shift
done

if test -d "$apr_src_dir"
then
  echo ""
  echo "Looking for apr source in $apr_src_dir"
else
  echo ""
  echo "Problem finding apr source in $apr_src_dir."
  echo "Use:"
  echo "  --with-apr=[directory]"
  exit 1
fi

# Remove some files, then copy them from apr source tree
rm -f build/apr_common.m4 build/find_apr.m4 build/install.sh \
      build/config.guess build/config.sub build/mkdir.sh \
      build/make_exports.awk build/make_var_export.awk \
      build/get-version.sh
cp $apr_src_dir/build/apr_common.m4 $apr_src_dir/build/find_apr.m4 \
   $apr_src_dir/build/install.sh $apr_src_dir/build/config.guess  \
   $apr_src_dir/build/config.sub $apr_src_dir/build/mkdir.sh \
   $apr_src_dir/build/make_exports.awk $apr_src_dir/build/make_var_export.awk \
   $apr_src_dir/build/get-version.sh \
   build

得到错误:

[root@localhost apr-iconv]# sh buildconf 
buildconf: line 2: $'\r': command not found
buildconf: line 6: $'\r': command not found
buildconf: line 10: syntax error near unexpected token `$'in\r''
'uildconf: line 10: `  case "$1" in

谢谢你:)

【问题讨论】:

  • 听起来您的文件是以 Windows 行结尾保存的。参见例如stackoverflow.com/questions/2613800/… 如何转换您的文件。
  • 嘿,非常感谢。将其转换为unix格式后,它就可以工作了。

标签: linux centos7


【解决方案1】:

一旦将 shell 脚本更改为 Unix 格式,此错误就会被清除。您可以通过执行命令dos2unix <file-name> 来完成该转换。如果必须安装 dos2unix 以避免出现错误“dos2unix: command not found”。

【讨论】:

    猜你喜欢
    • 2011-09-22
    • 2012-08-31
    • 2012-10-20
    • 2012-09-29
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    相关资源
    最近更新 更多