执行shell脚本时提示 /bin/bash^M: bad interpreter: No such file or directory
查看脚本文件是dos格式还是unix格式的几种办法。
(1)cat -A filename 从显示结果可以判断,dos格式的文件行尾为^M$,unix格式的文件行尾为$。
(2)od -t x1 filename 如果看到输出内容中存在0d 0a的字符,那么文件是dos格式,如果只有0a,则是unix格式。
(3)vi filename打开文件,执行 : set ff,如果文件为dos格式在显示为fileformat=dos,如果是unxi则显示为fileformat=unix。
这个错误主要是由于shell脚本文件是dos格式的,在windows系统下编写的代码每一行是以rn来换行,而unix格式的文件行尾则以n来标识。
解决的方法很简单就是把windows格式的文件转换为linux/unix的格式(简单讲就是把(rn)改成n格式)
解决方法:
第一种方式:
用notepad++打开文件,点“编辑/档案格式转换/转为UNIX格式“,保存并上传,问题搞定。
第二种方式:
使用直接使用 linux 命令 dos2unix filename,直接把文件转换为unix格式,有些系统要用 fromdos filename 命令
第三种方式:
用vi打开,:set fileformat=unix,和dos2unix一个意思
总之不管是那种方式,只要目的都是一样的把windows的文件转换为linux/UNIX 文件。
文件编码方式转变一下就可以了:
我编译脚本时虽然也是在linux上,但是我通过邮箱发送过来的,可能经过邮箱发送时被转化为windows格式了吧!
第四种方式:
[root@localhost bin]# dos2unix myshell.sh
dos2unix: converting file myshell.sh to UNIX format ...
第五种方式
vim mysell.sh
:get fileformat --->可以看到本文件的格式
:set fileformat=unix --->就可以将doc转化为unix格式了