shell脚本在linux下执行错误 -bash: ./a.sh: /bin/bash^M: bad interpreter: No such file or directory

在windows上写的脚本,在linux下执行发生错误

[root@hillin-cpp-dev-4 ~]# ./a.sh 
-bash: ./a.sh: /bin/bash^M: bad interpreter: No such file or directory

用vim a.sh进入a.sh这个文件, 然后在底部模式下, 执行:set ff查看一下, 结果发现fileformat=dos, 看看, 是文件格式问题

解决办法

方法一:
$ vim a.sh
在底部模式下, 执行:set fileformat=unix后执行:x或者:wq保存修改

方法二:
$ sed -i "s/\r//" a.sh
执行上述命令转化

方法三:
$ yum install -y dos2unix (centos安装dos2unix)
$ dos2unix a.sh
[root@hillin-cpp-dev-4 ~]# dos2unix a.sh
dos2unix: converting file a.sh to Unix format ...

shylin

猜你喜欢

转载自blog.csdn.net/Shyllin/article/details/81381502