Commit 973b577f authored by Matthieu Muffato's avatar Matthieu Muffato
Browse files

SystemCmd now accepts pipes and redirections in the command (when given an array)

parent 2a0ea6c1
......@@ -114,6 +114,26 @@ sub fetch_input {
}
=head2 text_to_shell_lit
Argument[0]: String
Description: Escapes the single-quotes of the string and wrap it into single-quotes
This is useful to stringify a list of commands / arguments to run them
through the shell.
NB: The "_" prototype is essential to allow the method to wrap $_ into @_
PS: Shamelessly adapted from http://www.perlmonks.org/?node_id=908096
=cut
my %shell_characters = map {$_ => 1} qw(< > |);
sub text_to_shell_lit(_) {
return $_[0] if $shell_characters{$_[0]} or $_[0] =~ /^[a-zA-Z0-9_\-]+\z/;
my $s = $_[0];
$s =~ s/'/'\\''/g;
return "'$s'";
}
=head2 run
Description : Implements run() interface method of Bio::EnsEMBL::Hive::Process that is used to perform the main bulk of the job (minus input and output).
......@@ -125,10 +145,17 @@ sub run {
my $self = shift;
my $cmd = $self->param('cmd');
my $flat_cmd = ref($cmd) ? join(' ', map { ($_=~/^-?\w+$/) ? $_ : "\"$_\"" } @$cmd) : $cmd;
my $flat_cmd = ref($cmd) ? join(' ', map text_to_shell_lit, @$cmd) : $cmd;
# system() can only spawn 1 process. For multiple commands piped
# together or if redirections are used, we need a shell. The system()
# way is to merge everything into a single string
$cmd = $flat_cmd if ref($cmd) and (grep {$shell_characters{$_}} @$cmd);
if($self->debug()) {
warn qq{cmd = "$flat_cmd"\n};
use Data::Dumper;
warn "Command used: ", Dumper($cmd);
}
$self->dbc and $self->dbc->disconnect_when_inactive(1); # release this connection for the duration of system() call
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment