shell 脚本中的“意外标记 '$'\r'' 附近的语法错误”

Eri*_*ric 4 bash

我正在编写脚本,但在第 2-3 行出现错误。错误是这样打印的:

./ex6.sh: line2: $'\r': command not found
./ex6.sh: line3: syntax error near unexpected token '$'\r''
./ex6.sh: line3: 'fund ()
Run Code Online (Sandbox Code Playgroud)

该文件名为ex6.sh. 有人可以就为什么会发生此错误提供一些指导吗?

#!/bin/sh

func () 
{
if [ "$1" == "" ]; then
echo "Default";
for i in `find` ; 
do
    if [ -d $i ]; then
        echo $i "is a directory";
    fi
    if [ -f $i ]; then
        if [ "$i" != "./script.sh" ]; then
            echo $i "is a file";        
        fi
    fi
done
fi

if [ "$1" == "--long" ]; then
echo "--long";
for i in `find` ; 
do
    if [ -d $i ]; then
        echo $i "is a directory";
    fi
    if [ -f $i ]; then
        echo $i "is a file";        
    fi
done
fi

if [ "$1" == "--rm" ]; then
echo "--rm";
for i in `find` ; 
do
    if [ -d $i ]; then
        echo $i "is a directory";
    fi
    if [ -f $i ]; then
        echo $i "is a file";        
    fi
done
fi
}

getArgs () {
if [ "$1" == "--long" ]; then
    echo "got the first param $1";
else
    if [ "$1" == "--rm" ]; then
        echo "got the second param $1";
    else
        if [ "$1" == "" ]; then
            echo "got default param";
        else
            echo "script.sh: unknown option $1";
            exit;
        fi  
    fi
fi

}

getArgs $1;
#ARGS=$1;

func $1;
Run Code Online (Sandbox Code Playgroud)

Dan*_*eck 7

使用dos2unix或适当的文本编辑器将所有换行符从 Windows 转换\r\n\n.