我正在编写一个脚本,在第 2-3 行出现错误。错误打印如下:
./ex6.sh: line2: $'\r': command not found
./ex6.sh: line3: syntax error near unexpected token '$'\r''
./ex6.sh: line3: 'fund ()
该文件名为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;
答案1
使用dos2unix
或适当的文本编辑器将所有换行符从 Windows 转换\r\n
为\n
。
答案2
检查文件中的行尾字符。可能您使用的是 Windows 样式 ( CR+ LF),而您应该使用的是 Unix/Linux 样式 ( 仅LF)。