是否有可能让'exec'在Perl中使用'$ SHELL -c'而不是'/ bin/sh -c'?

Wil*_*ell 3 unix perl exec

在Perl中,是否有可能使'exec','system'和'qx'使用除/ bin/sh之外的shell(不使用像'exec'这样的构造$ SHELL -c ..."',并且没有重新编译perl)?

编辑:这个问题的动机是一个bash脚本,它执行'export -f foo',然后在子shell中使用perl直接通过'system"foo"'调用函数.我不确定这种技术是否适用于所有sh,虽然'system'/ bin/bash -c foo"'可能在这种情况下有效,但我不希望导出的函数在/ bin /的所有变体中传播SH.但大多数时候我只是好奇,现在对如何将解决方案扩展到qx感到好奇.此外,由于我对非unix平台一无所知,所以我想避免在解决方案中对备用shell的路径进行硬编码.

Cha*_*ens 6

你可以覆盖execsystem.请参阅perldoc perlsub详细信息,但这里大致是您想要的(模拟一些引用错误,我不想尝试修复):

#!/usr/bin/perl

use strict;
use warnings;

use subs qw/system/;

sub system {
    #handle one arg version:
    if (@_ == 1) {
        return CORE::system "$ENV{SHELL} -c $_[0]";
    }
    #handle the multi argument version
    return CORE::system @_;
}

print "normal system:\n";
system "perl", "-e", q{system q/ps -ef | grep $$/};

print "overloaded system:\n";
system 'ps -ef | grep $$';
Run Code Online (Sandbox Code Playgroud)