开发者社区> 科技小能手> 正文

/usr/bin/perl^M: bad interpreter: No such file or directory

简介:
+关注继续查看

1、web显示

wKioL1kecrHB64O3AABlNqzDzOA696.png-wh_50

2、nagios服务端测试

[root@nagiosserver objects]# /usr/local/nagios/libexec/check_nrpe -H 10.0.0.10 -c check_mem
NRPE: Unable to read output


3、nagios客户端测试

[root@apache ~]#/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1-c check_mem
NRPE: Unable to read output

[root@apache ~]# /usr/local/nagios/libexec/check_memory.pl -w 6% -c 3%
-bash: /usr/local/nagios/libexec/check_memory.pl: /usr/bin/perl^M: bad interpreter: No such file or directory


4、解决方法:

[root@apache ~]# sed -i 's/\r$//' /usr/local/nagios/libexec/check_memory.pl
[root@apache ~]# /usr/local/nagios/libexec/check_memory.pl -w 6% -c 3%
CHECK_MEMORY OK - 384M free | free=403628032b;29877288.96:;14938644.48:


5、原因:

在*nix系统下使用Perl脚本有时会遇到如下错误:
/usr/bin/perl^M: bad interpreter: No such file or directory
最常见的原因是因为该脚本在windows系统下进行了编辑。
windows系统下的换行符是\r\n,而unix下面是只有\n的。如果要解决这个问题,只要去掉\r即可。


第一种解决方案是用sed(假设出问题的脚本名叫filename):
sed -i 's/\r$//' filename


这种解决方案适合简单的ASCII文件形式。
如果情况再复杂些,比方说filename是Unicode文件,可能会引入了Unicode中某些新的换行符;
另外有时候\r和\n在某些系统上对应的字符编码并不一致。如果再碰上垂直制表符 0x0B 和进纸符 0x0C 就更麻烦了。




好在Perl提供了另一种解决方案:
perl -p -i -e "s/\R/\n/g" filename


这里用到了从Perl 5.10开始引入的\R这个字符组,用来匹配各种换行符,我们只要方便地将其替换为\n就可以了。
同时也没有必要用脚本文件来实现,只需要在shell里执行这样一行命令就行了。
其中-p表示逐行对filename进行操作,-i表示原地操作,覆盖原始文件,-e则是表示执行后面的语句。



本文转自 sunrisenan 51CTO博客,原文链接:http://blog.51cto.com/sunrisenan/1927488

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
未解决:lrelease: could not exec ‘/usr/lib/qt5/bin/lrelease‘: No such file or directory
未解决:lrelease: could not exec ‘/usr/lib/qt5/bin/lrelease‘: No such file or directory
25 0
/usr/bin/xsltproc: not found
/usr/bin/xsltproc: not found
10 0
/usr/bin/sed: No such file or directory
/usr/bin/sed: No such file or directory
27 0
shell脚本 解决“/bin/bash^M: bad interpreter: No such file or directory”
shell脚本 解决“/bin/bash^M: bad interpreter: No such file or directory”
36 0
/usr/bin/env: escript: No such file or directory的解决办法
/usr/bin/env: escript: No such file or directory的解决办法
104 0
Error: /usr/lib64/libstdc++.so.6: version `CXXABI_1.3.8‘ not found (required by /usr/anaconda3/bin/)
Error: /usr/lib64/libstdc++.so.6: version `CXXABI_1.3.8‘ not found (required by /usr/anaconda3/bin/)
266 0
shell解决/bin/bash^M:bad interpreter:No such file or directory
在执行shell脚本时提示这样的错误主要是由于shell脚本文件是dos格式,即每一行结尾以\r\n来标识,而unix格式的文件行尾则以\n来标识。 查看脚本文件是dos格式还是unix格式的几种办法。
2303 0
文章
问答
文章排行榜
最热
最新
相关电子书
更多
The Next Gen of PHP
立即下载
Borgaonkar-New-Adventures-In-Spying-3G-And-4G-Users-Locate-Track-And-Monitor
立即下载
低代码开发师(初级)实战教程
立即下载