Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Perl won't run command with "|&"

by italdesign (Novice)
on Sep 19, 2013 at 22:19 UTC ( #1054916=perlquestion: print w/ replies, xml ) Need Help??
italdesign has asked for the wisdom of the Perl Monks concerning the following question:

Hello again monks. Please tell me what I'm doing wrong. Here's a piece of code to illustrate what I'm trying to do:
use strict; use warnings; my $cmd = qq{ /usr/bin/wget "http://www.google.com" }; $cmd .= q{ |& tee /path/to/tee.log }; my $result = `$cmd`; print "$result\n";
Running the script, I get the following error:
sh: -c: line 0: syntax error near unexpected token `&' sh: -c: line 0: `/usr/bin/wget "http://www.google.com" |& tee /path/t +o/tee.log '
Yet, if I copy and paste the EXACT command reported in the error msg (namely /usr/bin/wget "http://www.google.com"  |& tee /path/to/tee.log) and run it on the command line, it works just fine.

I've tried several variations of this, including using system() and exec() instead of ``, as well as escaping | and &. Nothing works.

I am running csh if that makes any difference.

Ultimately what I'm trying to do is run wget and have the output (STDERR + STDOUT) sent to both 1) a log file, as well as 2) a variable in my program. I've looked at Capture::Tiny but it seems to only accomplish one or the other.

Comment on Perl won't run command with "|&"
Select or Download Code
Replies are listed 'Best First'.
Re: Perl won't run command with "|&"
by ikegami (Pope) on Sep 19, 2013 at 23:06 UTC

    Perl uses /bin/sh. It would be useless if the syntax of the command had to vary based on what shell a user used on a particular day.

    You could always run csh from sh.

    use String::ShellQuote qw( shell_quote ); my $cmd = shell_quote('csh', '-c', 'wget http://... |& tee ...'); my $result = `$cmd`;

    Or avoid you can use the multi-arg open-pipe syntax to avoid sh and run csh directly.

    my $cmd = 'wget http://... |& tee ...'; open(my $pipe, '-|', '/bin/csh', '-c', $cmd) or die $!; my $result; { local $/; $result = <$pipe>; }

    Or you can adjust for sh.

    my $cmd = 'wget http://... 2>&1 | tee ...'; my $result = `$cmd`;
Re: Perl won't run command with "|&"
by aaron_baugher (Curate) on Sep 19, 2013 at 22:45 UTC

    You may be running csh as your shell, but perl appears to be using sh to run the command in backticks, according to the errors you're getting. I don't think sh has the |& syntax. This should combine STDOUT and STDERR and send the result to a file and to your backticks output:

    `wget "http://whatever" 2>&1 | tee /path/to/tee.log`

    Aaron B.
    Available for small or large Perl jobs; see my home node.

      Thanks guys!
Re: Perl won't run command with "|&"
by Anonymous Monk on Sep 19, 2013 at 22:59 UTC

    You have leading whitespace, why is that?

    Also, consider the output of perl -V:sh when you're writing your shell commands

    This is what you're really executing

    /bin/sh /usr/bin/wget "http://www.google.com" |& tee /path/to/tee.lo +g
    I think you should try  2>&1 | tee -a ...log

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1054916]
Approved by ikegami
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2015-07-30 23:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls