Shebang在文件标头中的使用:是否有任何选择在Shebang中通过多个参数

发布于 2025-01-26 08:55:50 字数 933 浏览 3 评论 0原文

我正在使用其他名称空间进行我的python文件执行。使用Shebang/Hashbang执行此脚本(/bin/bash -c)。

面临的问题是Shebang仅接受一个论点。即使我传递了多个ARG,它也将其视为一个字符串。

Shebang使用了: #!/sbin/ip netns exec exec tmp< oputable>

实验:

  1. 单个可执行文件使用:#!/sbin/ip

     对象“ TMP”未知,请尝试“ IP帮助”。
     
  2. tmp中的一个arg shebang:#!/sbin/ip netns

      $/bin/bash -c tmp
    命令“ TMP”是未知的,请尝试“ IP Netns帮助”。
     
  3. tmp中使用的两个arg shebang:#!/sbin/ip netns exec

      $/bin/bash -c tmp
    对象“ netns exec”未知,请尝试“ IP帮助”。
     
    • 其选择“ Netns Exec”作为单个参数
  4. 完整的cmd使用

    shebang在TMP中使用:#!/sbin/ip netns exec exec global python

      $/bin/bash -c tmp
    对象“ Netns Exec Global Python”是未知的,请尝试“ IP帮助”。
     

是否有任何方法可以将多个Argumenets传递给Shebang可执行文件以外的其他方法。

I am using a different namespace for my python file execution. using a shebang/hashbang, to execute this script (/bin/bash -c ).

problem faced is shebang only accepts a single argument. even if i pass multiple args its treating it as a single string.

shebang used:
#!/sbin/ip netns exec tmp <executable>

Experiment:

  1. single executable using : #!/sbin/ip

    Object " tmp" is unknown, try "ip help".
    
  2. one arg shebang in tmp : #!/sbin/ip netns

    $/bin/bash -c  tmp
    Command "tmp" is unknown, try "ip netns help".
    
  3. with two arg shebang used in tmp: #!/sbin/ip netns exec

    $/bin/bash -c  tmp
    Object "netns exec" is unknown, try "ip help".
    
    • its picking "netns exec" as a single argument
  4. full cmd used

    shebang used in tmp : #!/sbin/ip netns exec global python

    $/bin/bash -c  tmp
    Object "netns exec global python" is unknown, try "ip help".
    

Is there any way to pass multiple argumenets to shebang executable other than chaining files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别把无礼当个性 2025-02-02 08:55:51

Linux(我相信其他OSS)在第一个空格时只分开了一次Shebang String。因此,您只能通过一个论点。

当您执行#!/sbin/ip Netns Exec exec tmp&lt; ostable&gt;ip看到一个参数字符串:netns exec exec exec tmp&lt; dostable&gt;(包括空间)。

但是gnu env添加了用于拆分Shebang参数的-s选项。它也可以在FreeBSD env上使用。但不是busybox env

因此,如果您使用env -s

#!/usr/bin/env -S command -a -b -c

env> env将它们在空白空间上拆分三个参数,然后将它们传递给命令env当然正在看到单个参数-s命令-a -b -c,但可以解析此。

我对名称空间的了解不足以知道这是否是个好主意,但它似乎对我有用:

$ sudo ip netns add test
$ sudo ip netns identify
# (empty)

$ cat /tmp/netns-shebang-test
#!/usr/bin/env -S ip netns exec test bash

echo "$BASH_VERSION"
echo "$0"
ip netns identify

$ sudo /tmp/netns-shebang-test
5.1.16(1)-release
/tmp/netns-shebang-test-script
test

我显示出bash,但它也适用于类似的python脚本。我将重复一遍,我对名称空间不了解,无法知道这是否是一种很好的方法。

Linux (and other OSs I believe) only split the shebang string once, at the first whitespace. So you can only ever pass one argument.

When you do #!/sbin/ip netns exec tmp <executable>, ip sees one argument string: netns exec tmp <executable> (including spaces).

But GNU env added the -S option for splitting shebang arguments. It's also available on FreeBSD env. But not busybox env.

So if you launch your interpreter using env -S:

#!/usr/bin/env -S command -a -b -c

env will split the three arguments on white space, and pass them to command. env of course is seeing the single argument -S command -a -b -c, but it can parse this.

I don't know enough about namespaces to know if this is a good idea, but it seemed to work for me:

$ sudo ip netns add test
$ sudo ip netns identify
# (empty)

$ cat /tmp/netns-shebang-test
#!/usr/bin/env -S ip netns exec test bash

echo "$BASH_VERSION"
echo "$0"
ip netns identify

$ sudo /tmp/netns-shebang-test
5.1.16(1)-release
/tmp/netns-shebang-test-script
test

I'm showing bash, but it also worked with a similar python script. I will repeat that I don't know enough about namespaces to know if this is a good approach or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文