linux运行sh脚本报错$'\r': command not found

357 阅读1分钟

问题:

linux运行sh脚本报错$'\r': command not found

$ sudo sh ./jar.sh 
./jar.sh: line 2: $'\r': command not found
./jar.sh: line 6: $'\r': command not found
: No such file or directory
./jar.sh: line 10: $'\r': command not found
./jar.sh: line 13: $'\r': command not found
./jar.sh: line 20: $'\r': command not found
./jar.sh: line 72: syntax error near unexpected token `$'{\r''
'/jar.sh: line 72: `usage() {

原因:

这个脚本是在windows上编辑好后上传到linux上的,
而Windows系统的文件换行使用的是\r\n,而Linux系统是\n

解决:

  1. 使用vi编辑器执行命令:set ff=unix后保存即可
# vi jar.sh
# :set ff=unix
# x保存退出
  • 已测试,此方法有效

参考:
vim中文帮助:yianwillis.github.io/vimcdoc/doc…

  1. 安装dos2unix来进行文件转换
yum install -y dos2unix
dos2unix jar.sh

暂未测试