Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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 "" }; $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 "" |& tee /path/t +o/tee.log '
Yet, if I copy and paste the EXACT command reported in the error msg (namely /usr/bin/wget ""  |& 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.

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 "" |& tee /path/to/tee.lo +g
    I think you should try  2>&1 | tee -a ...log

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1054916]
Approved by ikegami
Front-paged by ikegami
usemodperl will try to make better shapes with his line noise, like a camel, or boobs, then it will be called "clever" :-)
Veltro Veltro translates line noise. It spells JAPH,
[usemodperl]: so... how do we get modules via HTTP from metacpan?
[marto]: configure whatever client you use to use a repositry you like?
[Corion]: usemodperl: Why would you want HTTP there? If you want/need to use wget without SSL libraries, set (or whatever) as your uplink?
[marto]: I suspect more trolling...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2018-06-24 16:11 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.