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

具体报错信息如下:

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

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

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

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

  1. [root@localhost test]# vi test.sh   

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

linux命令将doc转换为linux

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

linux命令将doc转换为linux

3.修改文件format为unix

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

命令:set ff=unix

linux命令将doc转换为linux

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

linux命令将doc转换为linux

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

  1. [root@localhost test]# dos2unix test.sh   
  2. dos2unix: converting file test.sh to UNIX format ...  

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

  1. [root@localhost test]# ./test.sh   
  2. test shell

相关文章:

  • 2022-02-25
  • 2021-10-17
  • 2021-12-11
  • 2021-11-03
  • 2021-10-24
  • 2021-12-25
  • 2022-12-23
  • 2021-09-12
猜你喜欢
  • 2021-10-11
  • 2021-09-10
  • 2022-03-01
  • 2021-10-31
  • 2021-09-23
  • 2022-12-23
  • 2022-01-07
相关资源
相似解决方案