Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External program called with backticks still produces output

Tags:

perl

backticks

so I call an external program in perl and want to capture it's output:

my @RNAalifoldOut = `RNAalifold some parameters`;

If called from command line the output consists of three lines, e.g:

4 sequences; length of alignment 48.
__GCCGA_UGUAGCUCAGUUGGG_AGAGCGCCAGACUGAAAAUCAGA 
...((((.....((((.........)))).(((((.......)))))

However my array @RNAalifoldOut contains only the two last lines and the first line appears directly on the screen when the line is being executed.

How can this be? I thought maybe the program writes the first line to STDERR, but isn't that discarded by the backticks operator? And what could I do to hide this output?

Regards Nick

like image 689
NiklasMM Avatar asked May 23 '12 18:05

NiklasMM


People also ask

What is the use of backtick in shell script?

In my Perl scripts, I would use the backtick operator to run a command in the operating system and return the output to continue the logic in the script. The backtick operator is also available in shell scripts, and because it is so easy to combine with other commands, I started using it a lot.

What does `` do in Linux?

It has a very special meaning. Everything you type between backticks is evaluated (executed) by the shell before the main command (like chown in your examples), and the output of that execution is used by that command, just as if you'd type that output at that place in the command line.

What is the use of backtick in Perl?

like system executes a command and your perl script is continued after the command has finished. In contrary to system the return value is STDOUT of the command. qx// is equivalent to backticks.

What is $? In Perl?

$? is the error code of the child process (perform_task.sh). In the case of your script the return code is shifted eight bits to the right and the result compared with 0. This means the run is considered a failure only if the returned code is > than 255.


1 Answers

You are likely seeing the standard error from RNAalifold. Backticks capture only the standard output.

Capture both standard output and standard error by changing your code to

my @RNAalifoldOut = `RNAalifold some parameters 2>&1`;

To discard the standard error, use

my @RNAalifoldOut = `RNAalifold some parameters 2>/dev/null`;

on Unix-like platforms. On Windows, use

my @RNAalifoldOut = `RNAalifold some parameters 2>nul`;
like image 93
Greg Bacon Avatar answered Nov 15 '22 05:11

Greg Bacon