Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

redirect output

by Sun751 (Beadle)
on Jul 17, 2009 at 06:56 UTC ( [id://780948]=perlquestion: print w/replies, xml ) Need Help??

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

I am writing a script which execute some commands and also runs third party script.
I am trying to redirect the output generated my 3rd party script and I came through this command,
perl test.pl >/dev/null
But if I do this, this will redirect the output I want to generate too,
for example I want "Processing going on..."
So can any one suggest me how can I redirect any output generate by script to >/dev/null rather than "Processing going on...". Any suggestion?Please.
Cheers,
Update.. For example,
I don't want any output generated by following commands,
open(EMAIL, "| $cmd")|| exe_err($cmd,'0','Email script');
And I want this output
print "Please Check Log: 'log.log' file\n"; exit;
Any suggestion? Cheers,

Replies are listed 'Best First'.
Re: redirect output
by ELISHEVA (Prior) on Jul 17, 2009 at 07:11 UTC

    The output you are "generating" is also getting redirected because both the output you want to "generate" and the output you want to throw away (to >/dev/null>) are being sent to STDOUT (standard out).

    The only reliable way to send output to two different places is to send it to two different places. You'll need to set up your script so that the output want to throw away is being sent to a different place, e.g. STDERR.

    If you aren't sure how to do this, you might want to update your original post with a bit of your code showing how the third party script and your other commands are being called. Be sure to add comments indicating the parts you consider "generated" and the parts you'd like to throw away to /dev/null

    Best, beth

Re: redirect output
by moritz (Cardinal) on Jul 17, 2009 at 07:01 UTC
    perl test.pl | grep 'Processing going on'

    Something along these lines?

Re: redirect output
by llancet (Friar) on Jul 17, 2009 at 08:47 UTC
    You should use the STDERR handle, which is pre-opened in every script. Generally, you may use it directly, and there is no need to open it.
    print STDERR "processing..\n"; print $result; print STDERR "done\n";
Re: redirect output
by Bloodnok (Vicar) on Jul 17, 2009 at 11:31 UTC
    As both ELISHEVA & llancet have pointed out, you _could_ use an alternate file handle/stream to receive the unwanted output, but IMO, this would have the dual disadvantages of...
    • Going against the grain and using STDERR for non error/warning output
    • Not being future proof - who knows what/which output my, or may not, be dispensable at some later point
    whereas moritzs grep based suggestion has neither disadvantage - at the notional cost of an additional run-time process.

    Just my 2 penn'orth :-)

    A user level that continues to overstate my experience :-))
Re: redirect output
by ig (Vicar) on Jul 18, 2009 at 16:39 UTC

    I would redirect the output of the sub-command rather than the calling script.

    use strict; use warnings; print "about to open cat in a sub-process\n"; open(CAT, '|-', "cat") or die "open cat failed: $!"; print CAT "This was written to cat without output redirected\n"; close(CAT); open(CAT, '|-', "cat > /dev/null") or die "open cat failed: $!"; print CAT "This was written to cat with output redirected\n"; close(CAT); print "all done\n";

    produces

    about to open cat in a sub-process This was written to cat without output redirected all done

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://780948]
Approved by ELISHEVA
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-19 20:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found