Why some commands are give different output when ran in Perl and in linux terminal

84 Views Asked by At

When I run this command:

bjobs -r -P xenon -W | awk '{print $7}' | grep -v JOB_NAME |
cut -f 1 -d ' ' | xargs

in a terminal, all running JOB_NAMES are coming, but when I do this in per_script only JOB_ID are coming.

Perl script code is below:

@dummy_jobs = qx/bjobs -r -P xenon -W | awk '{print $7}' | grep -v JOB_NAME | cut -f 1 -d ' ' | xargs/;

What needs to be changed in Perl?

1

There are 1 best solutions below

0
On

qx/.../ literals are very much like double-quoted strings. Specifically, $7 is interpolated, so you end up passing ... | awk '{print }' | ....

Replace

qx/...$7.../

with

qx/...\$7.../

Or if you prefer, you can use

my $shell_cmd = <<'EOS';  # These single-quotes means you get exactly what follows.
bjobs -r -P xenon -W | awk '{print $7}' | grep -v JOB_NAME | cut -f 1 -d ' ' | xargs
EOS

my @dummy_jobs = qx/$shell_cmd/;

Another difference is that qx uses /bin/sh instead of whatever shell you were using, but that shouldn't be relevant here.