相对shebang:如何编写运行随附的可移植解释器的可执行脚本 [英] Relative shebang: How to write an executable script running portable interpreter which comes with it

查看:85
本文介绍了相对shebang:如何编写运行随附的可移植解释器的可执行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个程序/程序包,它带有自己的解释器和一组脚本,这些脚本/程序应在执行时调用它(使用shebang).

Let's say we have a program/package which comes along with its own interpreter and a set of scripts which should invoke it on their execution (using shebang).

假设我们要保持其可移植性,因此即使简单地将其复制到其他位置(不同的机器),也无需调用安装/安装或修改环境(PATH)即可保持其功能.这些脚本不应混入系统解释器.

And let's say we want to keep it portable, so it remains functioning even if simply copied to a different location (different machines) without invoking setup/install or modifying environment (PATH). A system interpreter should not be mixed in for these scripts.

给定的约束条件排除了诸如shebang等具有绝对路径的已知方法:

The given constraints exclude both known approaches like shebang with absolute path:

#!/usr/bin/python 

并在环境中搜索

#!/usr/bin/env python

单独的发射器看上去很丑陋,是不可接受的.

Separate launchers look ugly and are not acceptable.

我发现了有关shebang限制的很好的摘要,它们描述了为什么shebang中的相对路径无用,并且解释器不能有多个参数:

I found good summary of the shebang limitations which describe why relative path in the shebang are useless and there cannot be more than one argument to the interpreter: http://www.in-ulm.de/~mascheck/various/shebang/

我还发现了大多数具有多行shebang"技巧的语言的实用解决方案.它允许编写如下脚本:

And I also found practical solutions for most of the languages with 'multi-line shebang' tricks. It allows to write scripts like this:

#!/bin/sh
"exec" "`dirname $0`/python2.7" "$0" "$@"
print copyright

但是有时候,我们不想扩展/修补现有的依赖shebang的脚本,而该脚本使用这种方法绝对可以解释.例如. Python的setup.py支持--executable选项,该选项基本上允许为其生成的脚本指定shebang内容:

But sometimes, we don't want to extend/patch existing scripts which rely on shebang with an absolute path to interpreter using this approach. E.g. Python's setup.py supports --executable option which basically allows to specify the shebang content for the scripts it produces:

python setup.py build --executable=/opt/local/bin/python

那么,特别是对于--executable=可以指定什么以实现所需的可移植性?或者换句话说,因为我想让这个问题不太针对于Python ...

So, in particular, what can be specified for --executable= in order to enable the desired kind of portability? Or in other words, since I'd like to keep the question not too specific to Python...

如何编写一个Shebang,它指定一个解释器,该解释器的路径相对于要执行的脚本的位置?

How to write a shebang which specifies an interpreter with a path which is relative to the location of the script being executed?

推荐答案

直接写在shebang中的相对路径相对于当前工作目录进行处理,因此#!../bin/python2.7之类的内容对于其他任何工作目录均不起作用

The relative path written directly in a shebang is treated relative to the current working directory, so something like #!../bin/python2.7 will not work for any other working directory except few.

由于OS不支持它,为什么不使用外部程序,例如使用env进行PATH查找.但是我不知道有专门的程序可以根据参数计算相对路径并执行生成的命令..除了外壳本身和其他脚本引擎之外.

Since OS does not support it, why not to use external program like using env for PATH lookup. But I know no specialized program which computes the relative paths from arguments and executes the resulting command.. except the shell itself and other scripting engines.

但是尝试在

#!/bin/sh -c '`dirname $0`/python2.7 $0'

不起作用,因为在Linux上,shebang仅受一个参数限制.这建议我寻找脚本引擎,该引擎接受脚本作为命令行上的第一个参数,并能够执行新流程:

does not work because on Linux shebang is limited by one argument only. And that suggested me to look for scripting engines which accept a script as the first argument on the command line and are able to execute new process:

#!/usr/bin/awk BEGIN{a=ARGV[1];sub(/[a-z_.]+$/,"python2.7",a);system(a"\t"ARGV[1])}

使用Perl

#!/usr/bin/perl -e$_=$ARGV[0];exec(s/\w+$/python2.7/r,$_)

这篇关于相对shebang:如何编写运行随附的可移植解释器的可执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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