0

I am usiing the following code to store the output of an external command in a variable. But when I print the error, I get nothing.

$error1 = `hadoop fs -copyFromLocal $src_dir $tgt_dir`;
print "$error1\n";  # --> prints nothing

The output of the command in ` ` is:

copyFromLocal: Cannot create file/user/file5._COPYING_. Name node is in safe mode.

Is there anything wrong in storing the output?

1

1 Answer 1

1

The backticks in Perl only capture standard output. If hadoop is sending the message to standard error instead, backticks won't capture it. See the perlfaq answer for How can I capture STDERR from an external command? for several ways to do it. The simplest is to redirect the standard error file descriptor into the standard output file descriptor with 2>&1:

$error1 = `hadoop fs -copyFromLocal $src_dir $tgt_dir 2>&1`;

Modules such as Capture::Tiny are very nice as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.