【问题标题】:Is there a way to bundle the bash interpreter along with the script?有没有办法将 bash 解释器与脚本捆绑在一起?
【发布时间】:2020-08-13 06:15:07
【问题描述】:

我想避免旧版本 bash 解释器的错误/行为,是否有解决方案将最近的(如 >4.3)bash 解释器与脚本捆绑在一起?

【问题讨论】:

  • 如果你使用比bash稳定得多的python等其他语言,可能会更容易。
  • 我认为可能有一种类似于答案combine script and zipoverwrite running script 中的方法。但是,您可能必须编译静态链接的 bash。此外,即使使用捆绑的 bash,您从脚本内部调用的工具(例如 grepsed...)仍然依赖于用户的平台。

标签: bash shell bundle interpreter


【解决方案1】:

编写用于特定版本的 shell 的脚本是个坏主意。编写符合 POSIX 的 shell 脚本,没那么难。

但是,您可以使用 shebang 在文件顶部声明脚本所需的解释器:

#!/path/to/interpreter

shell 脚本的一个常见(和推荐的)shebang 是:

#!/bin/sh 链接到系统外壳。

如果你想要一个特定的 shell,比如 bash,你可以这样写: #!/bin/bash

如果需要特定版本的 bash,您需要编写检查以验证存在的 bash 版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-08
    • 2018-07-03
    • 1970-01-01
    • 2023-03-16
    • 2022-09-27
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    相关资源
    最近更新 更多