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

解决Shell脚本执行错误
本文介绍了解决Shell脚本因文件格式问题导致无法执行的错误。通过使用vi编辑器和dos2unix命令将文件从DOS格式转换为UNIX格式,最终使Shell脚本能够正常运行。

Shell代码  

  1. #!/bin/sh  
  2. echo "test shell "  

具体报错信息如下:

Shell代码  

  1. [root@localhost test]# ./test.sh   
  2. -bash: ./test.sh: /bin/sh^M: bad interpreter: No such file or directory  

我们可以通过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   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值