简体   繁体   English

Shebang行与Perl的最大长度?

[英]max length of shebang line with Perl?

I've read somewhere that in Linux a shebang line ( #! ) can't exceed 127 characters. 我读过某个地方,在Linux中,shebang行( #! )不能超过127个字符。 I have a Perl script which has a shebang line of over 300 characters, and it hasn't failed (yet). 我有一个Perl脚本,该脚本的shebang行超过300个字符,并且没有失败(尚未)。 Does the limit apply only to the executable path (which is under 100 chars in my case), or is there some strange magic at work? 限制是否仅适用于可执行路径(在我的情况下,该路径小于100个字符),或者工作中是否存在一些奇怪的魔术?

Does the limit apply only to the executable path (which is under 100 chars in my case), or is there some strange magic at work? 限制是否仅适用于可执行路径(在我的情况下,该路径小于100个字符),或者工作中是否存在一些奇怪的魔术?

The limit applies only to the executable path because there is strange magic at work. 该限制仅适用于可执行路径,因为在工作中存在奇怪的魔术。 The strange magic goes by the name of Perl. 奇怪的魔术就是Perl的名字。

First, the limit of 127 characters is true. 首先,限制为127个字符。 (Or maybe 128 or 126, didn't actually count.) Everything past that gets truncated. (或者实际上可能不算128或126。)过去的所有内容都会被截断。 Second, regardless of whether there are spaces on the shebang line Linux will pass everything after the executable name as just ONE argument. 其次,不管shebang行上是否有空格,Linux都将可执行文件名之后的所有内容作为一个参数传递。 Third, Perl will parse the shebang line and interpret it itself, which is why this was working. 第三,Perl将解析shebang行并对其进行解释,这就是为什么这样做的原因。 But note that the effect of all this is strange and a bit perverse. 但是请注意,所有这一切的效果都是奇怪的,而且有些错误。 Suppose the shebang line looked like this: 假设shebang行如下所示:

#!/bin/perl -I/lib-one -I/lib-two

And suppose the truncation happened near the end, say, right before the "w" in "two" (that is, suppose the limit were 32 instead of 128, for ease of reading). 并假设该截断发生在末尾附近,例如,在“ two”中的“ w”之前(即,为了便于阅读,假设限制为32而不是128)。 Then the effect is as if you invoked perl at the command line as follows: 然后,效果就像您在命令行上按以下方式调用了perl一样:

shell-prompt$ /bin/perl "-I/lib-one -I/lib-t" -I/lib-one -I/lib-two

Which "works" in this case, but in general won't. 在这种情况下哪个“有效”,但通常不会。

I know this doesn't answer your original question about the maximum #! 我知道这并不能回答您最初关于最大#! line but I guess you rather have an XY problem . 线,但我想你宁愿有XY问题

Why don't you use lib ? 为什么不use lib If you have hard coded include paths in your script (in your shebang line) anyway then you can as well denote them with use lib statements and keep the shebang line short: 无论如何,如果您在脚本中(在shebang行中)都对路径进行了硬编码,那么您也可以use lib语句来表示它们,并保持shebang行简短:

#!/usr/bin/perl -some -absolutely -necessary -options

use lib '/lib-one';
use lib '/lib-two';
...
use lib '/lib-twenty-four';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM