意外标记 '$'in\r' 附近出现语法错误

amb*_*amb 3 linux centos7

当我构建 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
Run Code Online (Sandbox Code Playgroud)

获得错误:

[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
Run Code Online (Sandbox Code Playgroud)

谢谢 :)

amb*_*amb 5

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