我有一种情况需要检测特定的 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/