Perl:Shebang(空格?)"#!"? [英] Perl: Shebang (space?) "#! "?

查看:61
本文介绍了Perl:Shebang(空格?)"#!"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我都看过:

#!/path/...#!/小路/...

怎么了?有关系吗?有历史吗?

我听说一个古老的 Unix 版本要求没有空格.但后来我听说那只是谣言.有人确切知道吗?

<小时>

我想不出在哪里问这个更好.据我所知,它与编程有关,因为空间可以使程序以不同的方式运行.所以我在这里问.

解决方案

我也有一个模糊的记忆,在一些旧的类 Unix 系统中不允许使用空格,但一些研究不支持这一点.

根据这篇维基百科文章#! 语法于 1980 年 1 月在第 8 版 Unix 中引入.丹尼斯·里奇 (Dennis Ritchie) 最初宣布此功能时说:

<块引用>

系统已更改,如果正在执行的文件开始使用魔术字符 #!,该行的其余部分被理解为是已执行文件的解释器的名称.以前(和实际上仍然)外壳完成了大部分工作;它自动在文本文件中以可执行模式在文本文件上执行自身文件名是作为命令输入的.将设施放入系统具有以下优点.

[剪辑]

<块引用>

为了利用这个绝好的机会,把

#!/bin/sh

在 shell 脚本第一行的左边距.空白! 之后就OK了.使用完整的路径名(不进行搜索).在整行被限制为 16 个字符,但这个限制将被提升.

可以想象,一些后来的类 Unix 系统支持 #! 语法但不允许 ! 后面有空格,但考虑到第一个实现明确允许空白,这似乎不太可能.

leonbloy 的回答提供了更多背景信息.

更新:

Perl 解释器本身识别以 #! 开头的一行,即使在内核无法识别的系统上也是如此.运行 perldoc perlrun 或查看此网页了解详情.><块引用>

#!当线路正在运行时,始终检查线路是否有开关解析.因此,如果你在一台只允许一个参数的机器上与 #!行,或者更糟的是,甚至无法识别 #!线,你不管 Perl 是怎样的,仍然可以获得一致的切换行为调用,即使 -x 用于查找程序的开头.

Perl 也允许在 #! 之后有空格.

(就我个人而言,我更喜欢编写没有空格的 #! 行,但无论哪种方式都可以.)

leonjoy 的回答指向此网页由 Sven Mascheck 提供,其中讨论了 #!深入.(我现在提到这个是因为最近关于 comp.unix.shell 的讨论.)

I've seen both:

#!/path/...
#! /path/...

What's right? Does it matter? Is there a history?

I've heard that an ancient version of Unix required there not be a space. But then I heard that was just a rumor. Does anyone know for certain?


Edit: I couldn't think where better to ask this. It is programming related, since the space could make the program operate in a different way, for all I know. Thus I asked it here.

解决方案

I also have a vague memory that whitespace was not allowed in some old Unix-like systems, but a bit of research doesn't support that.

According to this Wikipedia article, the #! syntax was introduced in Version 8 Unix in January, 1980. Dennis Ritchie's initial announcement of this feature says:

The system has been changed so that if a file being executed begins with the magic characters #!, the rest of the line is understood to be the name of an interpreter for the executed file. Previously (and in fact still) the shell did much of this job; it automatically executed itself on a text file with executable mode when the text file's name was typed as a command. Putting the facility into the system gives the following benefits.

[SNIP]

To take advantage of this wonderful opportunity, put

#! /bin/sh

at the left margin of the first line of your shell scripts. Blanks after ! are OK. Use a complete pathname (no search is done). At the moment the whole line is restricted to 16 characters but this limit will be raised.

It's conceivable that some later Unix-like system supported the #! syntax but didn't allow blanks after the !, but given that the very first implementation explicitly allowed blanks, that seems unlikely.

leonbloy's answer provides some more context.

UPDATE :

The Perl interpreter itself recognizes a line starting with #!, even on systems where that's not recognized by the kernel. Run perldoc perlrun or see this web page for details.

The #! line is always examined for switches as the line is being parsed. Thus, if you're on a machine that allows only one argument with the #! line, or worse, doesn't even recognize the #! line, you still can get consistent switch behaviour regardless of how Perl was invoked, even if -x was used to find the beginning of the program.

Perl also permits whitespace after the #!.

(Personally, I prefer to write the #! line without whitespace, but it will work either way.)

And leonjoy's answer points to this web page by Sven Mascheck, which discusses the history of #! in depth. (I mention this now because of a recent discussion on comp.unix.shell.)

这篇关于Perl:Shebang(空格?)&quot;#!&quot;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆