加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 为什么shebang线总是第一行?

发布时间:2020-12-15 21:12:18 所属栏目:大数据 来源:网络整理
导读:我有简单的perl脚本如下: #!/usr/bin/perluse strict;use warnings;print "hello ! worldn"; 我可以执行这个脚本如下: temp.plhello ! world 如果我添加一些这样的评论: #this script is just for test#the shebang#!/usr/bin/perluse strict;use warnin
我有简单的perl脚本如下:
#!/usr/bin/perl

use strict;
use warnings;

print "hello ! worldn";

我可以执行这个脚本如下:

>temp.pl
hello ! world
>

如果我添加一些这样的评论:

#this script is just for test
#the shebang
#!/usr/bin/perl

use strict;
use warnings;

print "hello ! worldn";

当我尝试执行它给我的输出如下:

> temp.pl
use: Command not found.
use: Command not found.
print: Command not found.
>

这里的一点是shebang线应该永远在顶端,不管是什么.
但是有人可以解释一下为什么吗?

解决方法

shebang必须是第一行,因为它由内核解释,内核会查看可执行文件开头的两个字节.如果这些是#!该行的其余部分被解释为可运行的可执行文件,该脚本文件可用于该程序. (细节略有不同,但这是图片).

由于内核只会看前两个字符,并且没有更多行的概念,所以必须将hash bang放在第1行中.

现在如果内核无法执行以#!开头的文件怎么办? shell尝试分配可执行文件并被内核通知无法执行程序,最后的手段会将文件内容解释为shell脚本.由于shell不是perl,您会收到一堆错误,就像您尝试运行一样

sh < temp.pl

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读