perl - 有条件的 Perl Shebang?

标签 perl

我有一种情况需要检测特定的 perl 可执行文件 /usr/goofy/bin/perl 是否存在,如果存在则使用它来运行 Perl 脚本,否则使用 /usr/bin/perl.

我一直在为这个名为 perlshebang.pl 的小 POC 脚本苦苦挣扎:

#!/bin/sh -e
perls="/usr/goofy/bin/perl /usr/bin/perl"
for pl_exec in $perls
do
    if [ -x $pl_exec ]; then
        exec "$pl_exec -w -S \"$0\" ${1+\"$@\"}"
    fi
done

print "[$^X] Whoop!\n";

当我在没有 /usr/goofy/bin/perl 的系统上运行它时,我得到了这个错误信息:

./perlshebang.pl: 6: exec: /usr/bin/perl -w -S "./perlshebang.pl" : not found

当我在有 /usr/goofy/bin/perl 的系统上运行它时,我收到类似的错误消息:

./perlshebang.pl: line 6: /usr/goofy/bin/perl -w -S "./perlshebang.pl" : No such file or directory

我想我已经接近了,但无法弄清楚为什么我会收到这些错误消息。

谢谢!

最佳答案

要回答您的问题“为什么我会收到这些错误消息?”,问题出在您的 exec 行:

exec "/path/to/cmd arg arg"
# This will attempt to execute a file named "cmd arg arg"
# (with spaces in name) in directory /path/to/

对比

exec /path/to/cmd arg arg
# This will attempt to execute a file named "cmd" in directory
# /path/to/, with arguments "arg" and "arg"

所以,这就是 shell 提示找不到您的可执行文件的原因。您没有名为 perl -w -s "perlshebang.pl" 的文件,既不在 /usr/bin/ 下,也不在 /usr/goofy/下bin/.

关于perl - 有条件的 Perl Shebang?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35763596/

相关文章:

perl - 如何在 Perl 中读取 Little-endian UTF-16 Unicode 文本?

perl - 如何将充满 unix 时间字符串的文件转换为人类可读的日期?

csv - 读取CSV解析数据并存储在Hash中

perl - 从 CLI 的模块调用 Perl 函数

linux - 在 Redhat Linux 64 位上构建 DBD::SQlite

Perl 模块层次结构和组合

perl - 如何从 perl 访问 HTTP 基本身份验证用户名?

perl - 如何让 perl -c 抛出未定义或未声明的函数错误?

perl - 只从 perl 中的列表中获取某些值

perl - 将在子进程中执行的进程的值返回给父进程