bash 和 linux 内核中的 Shebang 行限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10813538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:24:37  来源:igfitidea点击:

Shebang line limit in bash and linux kernel

linuxbashshebang

提问by sortega

I'm trying to execute python scripts automatically generated by zc.buildout so I don't have control over them. My problem is that the shebang line (#!) is too long for either bash (80 character limit) or direct execution (some Linux kernel constant I don't know).

我正在尝试执行由 zc.buildout 自动生成的 python 脚本,因此我无法控制它们。我的问题是shebang行(#!)对于bash(80个字符限制)或直接执行(我不知道的一些Linux内核常量)来说太长了。

This is an example script to help you reproduce my problem:

这是一个示例脚本,可帮助您重现我的问题:

#!/././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././././bin/bash
echo Hola!

How can be bash or the kernel configured to allow for bigger shebang lines?

如何配置 bash 或内核以允许更大的 shebang 行?

回答by Ignacio Vazquez-Abrams

Limited to 127 chars on 99.9% of systems due to kernel compile time buffer limit.

由于内核编译时缓冲区限制,在 99.9% 的系统上限制为 127 个字符。

It's limited in the kernel by BINPRM_BUF_SIZE, set in include/linux/binfmts.h.

它在内核中受限于BINPRM_BUF_SIZE,在 中设置include/linux/binfmts.h

回答by glenn Hymanman

If you don't want to recompile your kernel to get longer shebang lines, you could write a wrapper:

如果您不想重新编译内核以获得更长的 shebang 行,您可以编写一个包装器:

#!/bin/bash

if [[ $# -eq 0 ]]; then
    echo "usage: ${0##*/} script [args ...]"
    exit
fi

# we're going to expand a variable *unquoted* to use word splitting, but
# we don't want to have path expansion effects, so turn that off
set -f

shebang=$(head -1 "")
if [[ $shebang == '#!'* ]]; then
    interp=( ${shebang#\#!} )        # use an array in case a argument is there too
else
    interp=( /bin/sh )
fi

# now run it
exec "${interp[@]}" "$@"

and then run the script like: wrapper.sh script.sh

然后运行脚本,如: wrapper.sh script.sh

回答by coderfi

Updated @glenn Hymanman's script to support passing in command line arguments.

更新了@glenn Hymanman 的脚本以支持传入命令行参数。

Incidentally, I ran into this problem when creating a python virtualenv inside of a very deep directory hierarchy.

顺便说一句,我在非常深的目录层次结构中创建 python virtualenv 时遇到了这个问题。

In my case, this was a virtualenv created inside a Mesos framework dir.

就我而言,这是在 Mesos 框架目录中创建的 virtualenv。

The extra long shebang rendered calling xxx/.../venv/bin/pipuseless.

超长的shebang使调用变得xxx/.../venv/bin/pip无用。

The wrapper script proved most useful.

包装脚本被证明是最有用的。

#!/usr/bin/env bash

script="" 
shebang=$(head -1 "$script")

# use an array in case a argument is there too
interp=( ${shebang#\#!} )        

# now run it, passing in the remaining command line arguments
shift 1
exec "${interp[@]}" "$script" "${@}"