ewhitt has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to suppress the output from system?
system('tar -vxf upload.tar');
Thanks!

Replies are listed 'Best First'.
Re: Disable output from system
by Anonymous Monk on Mar 26, 2009 at 04:42 UTC
    Don't ask for output :)
    | | V system('tar -vxf upload.tar'); ^ | |
Re: Disable output from system
by targetsmart (Curate) on Mar 26, 2009 at 04:32 UTC
    On unix system using bash(shell), one way of suppressing error and output
    tar -vxf upload.tar 2>&1 >/dev/null

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.

      I think you mean:

      tar -vxf upload.tar >/dev/null 2>&1

      -- 
      Ronald Fischer <ynnor@mm.st>
Re: Disable output from system
by rovf (Priest) on Mar 26, 2009 at 08:54 UTC

    In addition to what already has been said, here other variations:

    qx(tar ...); # Burns more CPU cycles, but who cares ;-) use File::Spec::Functions qw(devnull); system('tar .... >'.devnull.' 1>&2'); # works on *nix and windoze

    -- 
    Ronald Fischer <ynnor@mm.st>
      Hi, Roland. You wrote: system('tar .... >'.devnull.' 1>&2'); # works on *nix and windoze As I can see you redirect STDOUT to devnull and then again you redirect STDOUT to STDERR... Is this correct? I think it is typo, isn't it?
        you redirect STDOUT to devnull and then again you redirect STDOUT to STDERR... Is this correct?

        Sorry, a typo indeed. Of course you have to do something like

        '....>'.devnull.' 2>&1'
        and not "1>&2". I mistakingly exchanged 2 and 1. My point was that you have to redirect *first* to devnull, and *then* do the 2>&1, not the other way around.

        -- 
        Ronald Fischer <ynnor@mm.st>
      Hi, Roland. You wrote: system('tar .... >'.devnull.' 1>&2'); # works on *nix and windoze As I can see you redirect STDOUT to devnull and then again you redirect STDOUT to STDERR... Is this correct? I think it is typo, isn't it ?
Re: Disable output from system
by educated_foo (Vicar) on Mar 26, 2009 at 22:04 UTC
    You can dup stdout and stderr, then close them:
    sub stfu_system { local (*OUT, *ERR); open OUT, ">&STDOUT"; open ERR, ">&STDERR"; close STDOUT; close STDERR; system @_; open STDOUT, ">&OUT"; open STDERR, ">&ERR"; }

      This will work for many commands but some will fail if they don't have STDOUT and/or STDERR open.

        True. For those (poorly-written) commands, s!close\s+(\w+)!open $1, '>/dev/null'!