10

【linux】执行shell脚本出现异常:bad interpreter- no such file or directory如何解...

 3 years ago
source link: https://itimetraveler.github.io/2016/04/29/%E3%80%90Linux%E3%80%91%E6%89%A7%E8%A1%8Cshell%E8%84%9A%E6%9C%AC%E5%87%BA%E7%8E%B0%E5%BC%82%E5%B8%B8%EF%BC%9Abad%20interpreter-%20No%20such%20file%20or%20directory%E5%A6%82%E4%BD%95%E8%A7%A3%E5%86%B3/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【Linux】执行shell脚本出现异常:bad interpreter- No such file or directory如何解决

在Linux中执行.sh脚本,异常/bin/bash^M: bad interpreter: No such file or directory.

一、分析

这是不同系统编码格式引起的:在windows系统中编辑的.sh文件可能有不可见字符,所以在Linux系统下执行会报以上异常信息。

二、解决

1)在windows下转换:
利用一些编辑器如UltraEdit或EditPlus等工具先将脚本编码转换,再放到Linux中执行。转换方式如下(UltraEdit):File–>Conversions–>DOS->UNIX即可。

2)直接在Linux中转换(推荐做法):

首先要确保文件有可执行权限

#sh> chmod a+x filename

然后修改文件格式

#sh> vi filename

利用如下命令查看文件格式

:set ff 或 :set fileformat

可以看到如下信息

fileformat=dos 或 fileformat=unix

利用如下命令修改文件格式

:set ff=unix 或 :set fileformat=unix 
:wq (存盘退出)

最后再执行文件

#sh>./filename

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK