首页 > 系统服务 > 详细

shell脚本报错:-bash: xxx: /bin/bash^M: bad interpreter: No such file or directory

时间:2017-02-22 16:47:49      阅读:241      评论:0      收藏:0      [点我收藏+]

今天写了一个shell脚本,然后在执行的时候报错,脚本内容很简单,仅供测试:

Shell代码  技术分享
  1. #!/bin/sh  
  2.   
  3. echo "test shell "  

具体报错信息如下:

Shell代码  技术分享
  1. [root@localhost test]# ./test.sh   
  2. -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory  

主要原因是test.sh是我在windows下编辑然后上传到linux系统里执行的。.sh文件的格式为dos格式。而linux只能执行格式为unix格式的脚本。

 

我们可以通过vi编辑器来查看文件的format格式。步骤如下:

1.首先用vi命令打开文件

Shell代码  技术分享
  1. [root@localhost test]# vi test.sh   

2.在vi命令模式中使用 :set ff 命令

技术分享

可以看到改文件的格式为dos

技术分享

3.修改文件format为unix

方法一:使用vi修改文件format

命令:set ff=unix

技术分享

执行完后再通过set ff命令查看文件格式,结果如下:

技术分享

方法二:直接使用dos2unix命令修改

Shell代码  技术分享
  1. [root@localhost test]# dos2unix test.sh   
  2. dos2unix: converting file test.sh to UNIX format ...  

 修改完后再次执行./test.sh,执行正确:

Shell代码  技术分享
  1. [root@localhost test]# ./test.sh   
  2. test shell   

shell脚本报错:-bash: xxx: /bin/bash^M: bad interpreter: No such file or directory

原文:http://www.cnblogs.com/zyb-pp/p/6429448.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!