shell腳本報錯:-bash: xxx: /bin/sh^M: bad interpreter: No such file or directory
- #!/bin/sh
- echo "test shell "
具體報錯信息如下:
- [root@localhost test]# ./test.sh
- -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命令打開文件
- [root@localhost test]# vi test.sh
2.在vi命令模式中使用 :set ff 命令
可以看到改文件的格式為dos
3.修改文件format為unix
方法一:使用vi修改文件format
命令:set ff=unix
執行完后再通過set ff命令查看文件格式,結果如下:
方法二:直接使用dos2unix命令修改
- [root@localhost test]# dos2unix test.sh
- dos2unix: converting file test.sh to UNIX format ...
修改完后再次執行./test.sh,執行正確:
- [root@localhost test]# ./test.sh
- test shell