perl backticks: use bash instead of sh

2020-03-03 06:04发布

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 bash?

PS. The command that I'm trying to run is:

paste filename <(cut -d \" \" -f 2 filename2 | grep -v mean) >> filename3

6条回答
孤傲高冷的网名
2楼-- · 2020-03-03 06:07

Create a perl subroutine:

sub bash { return `cat << 'EOF' | /bin/bash\n$_[0]\nEOF\n`; }

And use it like below:

my $bash_cmd = 'paste filename <(cut -d " " -f 2 filename2 | grep -v mean) >> filename3';
print &bash($bash_cmd);

Or use perl here-doc for multi-line commands:

$bash_cmd = <<'EOF';
    for (( i = 0; i < 10; i++ )); do
       echo "${i}"
    done
EOF
print &bash($bash_cmd);
查看更多
欢心
3楼-- · 2020-03-03 06:16

To deal with running bash and nested quotes, this article provides the best solution: How can I use bash syntax in Perl's system()?

my @args = ( "bash", "-c", "diff <(ls -l) <(ls -al)" );
system(@args);
查看更多
Emotional °昔
4楼-- · 2020-03-03 06:17

Try

`bash -c \"your command with args\"`

I am fairly sure the argument of -c is interpreted the way bash interprets its command line. The trick is to protect it from sh - that's what quotes are for.

查看更多
▲ chillily
5楼-- · 2020-03-03 06:20

I thought perl would honor the $SHELL variable, but then it occurred to me that its behavior might actually depend on your system's exec implementation. In mine, it seems that exec

will execute the shell (/bin/sh) with the path of the file as its first argument.

You can always do qw/bash your-command/, no?

查看更多
贼婆χ
6楼-- · 2020-03-03 06:25

This example works for me:

$ perl -e 'print `/bin/bash -c "echo <(pwd)"`'
/dev/fd/63
查看更多
做个烂人
7楼-- · 2020-03-03 06:26

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`;
查看更多
登录 后发表回答