Is it possible to use 'exec' to use '$ SHELL -c' instead of '/ bin / sh -c' in Perl?

In Perl, you can make "exec", "system" and "qx" use a shell other than / bin / sh (without using a construct like "exec" $ SHELL -c ... "', and without recompiling perl)?

EDIT: The motivation for this question is a bash script that runs 'export -f foo' and then uses perl in a subshell to invoke a function directly through 'system' foo. ”I'm not sure if this technique will work with all sh, and although "system" / bin / bash -c foo "may work in this scenario, I would not expect the exported function to propagate through all the / bin / sh variants. But basically, I was just curious, and now I'm interested in learning how to expand the solution to qx. Also, since I know nothing about platforms other than unix, I would like to avoid hard-coding the path to an alternative shell in the solution.

+3
source share
4 answers

exec system. . 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 $$';
+6

exec system (, , /bin/sh , UNIX), . ( perlfunc)

IPC::Run3 system

+3

"exec" $SHELL -c... "? , exec , . , .:)

sub my_exec {
    exec $ENV{SHELL}, '-c', @_;
    }

, , - $ENV {SHELL}, script, . , , /etc/shells - . , , , , , .

+3

exec /bin/sh

. .

, , .

-5

Source: https://habr.com/ru/post/1714718/


All Articles