Shell—常见报错问题

bash:$'\r': command not found

造成这个问题的原因是Windows环境下换行的“\r”到了Linux环境下不能够识别了,因为Linux环境下默认的换行符为“\n”,我们只需要把文件转成unix就好。使用 dos2unix 工具实现该转换。

# 1.安装 dos2unix 工具
[root@localhost ~]# apt install dos2unix         # Ubuntu系统
[root@localhost ~]# yum -y install dos2unix   # Centos系统

# 2.转换文件
[root@localhost ~]# chmod 755 test.sh
[root@localhost ~]# dos2unix test.sh

# 3.再次执行文件就可以啦
[root@localhost ~]# sh ./test.sh

执行shell报bad interpreter: No such file or directory错误

原因:.sh脚本在windows系统下编写的,所以可能有不可见字符,从上图错误提示来看,很有可能脚本文件是DOS格式的,即每一行的行尾以\r\n来标识, 其ASCII码分别是0x0D, 0x0A。

1.通过vim filename 或者vi filename 进入编辑,

2.然后通过shift+:进入命令模式。接着通过命令set ff 或者 set fileformat 查看文件格式,这里显示的是fileformat=dos。

3.修改文件格式dos为unix。通过命令set ff=unix 或 set fileformat=unix修改,然后再执行set ff 或者 set fileformat,查看是否修改成功。

4.重新启动并运行命令,发现正常启动,至此完毕。

  

posted @ 2019-11-16 15:07  刘_love_田  阅读(838)  评论(0编辑  收藏  举报