/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

相关文章
|
5月前
|
Unix Shell iOS开发
Shell错误:/bin/bash^M: bad interpreter: No such file or directory
Shell错误:/bin/bash^M: bad interpreter: No such file or directory
72 0
|
移动开发 Unix Shell
/bin/bash^M: bad interpreter: No such file or directory
/bin/bash^M: bad interpreter: No such file or directory
109 0
未解决: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
230 0
Can‘t exec “aclocal“: 没有那个文件或目录 at /usr/share/autoconf/Autom4te/FileUtils.pm line 326.
Can‘t exec “aclocal“: 没有那个文件或目录 at /usr/share/autoconf/Autom4te/FileUtils.pm line 326.
324 0
Can‘t exec “autopoint“: 没有那个文件或目录 at /usr/share/autoconf/Autom4te/FileUtils.pm line 345.
Can‘t exec “autopoint“: 没有那个文件或目录 at /usr/share/autoconf/Autom4te/FileUtils.pm line 345.
456 0
|
Perl
/usr/bin/sed: No such file or directory
/usr/bin/sed: No such file or directory
166 0
|
移动开发 Unix Shell
shell脚本 解决“/bin/bash^M: bad interpreter: No such file or directory”
shell脚本 解决“/bin/bash^M: bad interpreter: No such file or directory”
125 0
|
Unix Linux Shell
shell解决/bin/bash^M:bad interpreter:No such file or directory
在执行shell脚本时提示这样的错误主要是由于shell脚本文件是dos格式,即每一行结尾以\r\n来标识,而unix格式的文件行尾则以\n来标识。 查看脚本文件是dos格式还是unix格式的几种办法。
2487 0