Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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
[tobyink]: 1nickt: your code?
[LanX]: pryrt: yeah, that's why I didn't consider, but the last >10 anonymous posts are from the same troll-person
[jdporter]: is there a module for expanding tabs in text?
[jdporter]: A: yes.
[Lady_Aleena]: I don't know how hard it will be to get out of that mess.
[LanX]: M-x untabify
[choroba]: I don't think they're multiple people. I was told "he or she" sounds old-fashioned and "they" is the way to say it
[pryrt]: Those others were definitely offensive or unequivocably rude, I agree.
[choroba]: (well, it comes from the 14th century, so labelling it as "modern" doesn't seem appropriate)
[Your Mother]: "They" is becoming accepted but it irritates me sometimes. I tend to just pick she or he randomly or try to use "one."

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (16)
As of 2017-05-24 20:20 GMT
Find Nodes?
    Voting Booth?