最近写了一个shell脚本,,是在Windows系统下写的,编写完成后复制到Linux系统中执行时报错了,大概是这样,,,

1
2
3
4
5
6
7
8
build.sh: line 3: $'\r': command not found
: invalid option set: -
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
build.sh: line 6: $'\r': command not found
build.sh: line 11: $'\r': command not found
build.sh: line 18: $'\r': command not found
build.sh: line 20: syntax error near unexpected token `$'{\r''
'ild.sh: line 20: `setup_install() {

原因是换行符格式问题导致的,Windows系统中用的是 \r\n 换行,Linux系统则用 \n,在Linux 系统中会将 Windows风格的换行符中的 \r 视为普通字符,从而导致错误,,

解决方法很简单,使用sed 删除每一行末尾的 \r 就可以了

1
sed -i -e 's/\r$//' build.sh

或者用dos2unix工具进行转换,dos2unix build.sh