perl backticks: use bash instead of sh

前端 未结 6 1638
南笙
南笙 2021-01-05 11:20

I noticed that when I use backticks in perl the commands are executed using sh, not bash, giving me some problems.

How can I change that behavior so perl will use ba

6条回答
  •  既然无缘
    2021-01-05 12:06

    The "system shell" is not generally mutable. See perldoc -f exec:

    If there is more than one argument in LIST, or if LIST is an array with more than one value, calls execvp(3) with the arguments in LIST. If there is only one scalar argument or an array with one element in it, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is "/bin/sh -c" on Unix platforms, but varies on other platforms).

    If you really need bash to perform a particular task, consider calling it explicitly:

    my $result = `/usr/bin/bash command arguments`;
    

    or even:

    open my $bash_handle, '| /usr/bin/bash' or die "Cannot open bash: $!";
    print $bash_handle 'command arguments';
    

    You could also put your bash commands into a .sh file and invoke that directly:

    my $result = `/usr/bin/bash script.pl`;
    

提交回复
热议问题