windows下编辑shell脚本,拷贝到linux下执行报错

1、报错信息

bad interpreter: No such file or directory 或者 syntax error near unexpected token `xxxxxxx\r''

这两个报错信息分别是./ 执行脚本和sh执行脚本打印的报错信息(关于同一个脚本,为什么这两个执行方式,报错信息不一样,有兴趣的小伙伴可以自己思考下)

[root@localhost ~]# ./h.sh 
-bash: ./h.sh: /bin/bash^M: bad interpreter: No such file or directory
[root@localhost ~]# 
[root@localhost ~]# sh h.sh 
h.sh: line 4: syntax error near unexpected token `$'do\r''
'.sh: line 4: `do
[root@localhost ~]# 

 

2、打开脚本查看文件格式

vim打开文件后,进入末行模式,查看文件格式"set ff"

[root@localhost ~]# vim h.sh 

#!/bin/bash
db="a b c d e"
for i in $db
do
  echo $i
done
~                                                                                                                                                                                           
~                                                                                                                                                                                           
~                                                                                                                                                                                           
~                                                                                                                                                                                           
~                                                                                                                                                                                           
                                                                                                                                                                                          
                                                                                                                                                                                      
~                                                                                                                                                                                           
~                                                                                                                                                                                           
:set ff

发现脚本文件是DOS格式的,即每一行的行尾以\r\n来标识。

修改文件格式,进入末行模式,"set ff=unix" ,然后使用set ff 查看修改后文件格式是否为 ”fileformat=unix “,确认没有问题后保存退出。

执行脚本验证是否执行成功。

[root@localhost ~]# ./h.sh 
a
b
c
d
e
[root@localhost ~]# 
[root@localhost ~]# sh h.sh 
a
b
c
d
e
[root@localhost ~]# 
posted @ 2021-09-23 16:33  刘铁柱柱柱柱柱柱  阅读(342)  评论(0编辑  收藏  举报