Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Keep It Simple, Stupid
 
PerlMonks

redirecting standard output and standard error

by s_gaurav1091 (Beadle)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Sep 04, 2006 at 12:25 UTC ( #571072=perlquestion: print w/ replies, xml ) Need Help??
s_gaurav1091 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am struck up at a problem. I want to write a script in which I dont any error to be shown on the console.Pls look at the code for refrence.
my $rc = system("java -version 2>/dev/null"); print "$rc\n";
Now when I am running the srcipt and in case the error comes I want only $rc should be printed on screen. But the output is coming like UX:sh (sh): ERROR: java: Not found rc is 256 Can anyone pls sugget as how to hide this error.Thanx in advance

Comment on redirecting standard output and standard error
Download Code
Re: redirecting standard output and standard error
by shmem (Canon) on Sep 04, 2006 at 12:55 UTC
    my $rc = system("java -version >/dev/null 2>&1");
    might do on UNIX/Linux (redirect STDOUT to /dev/null, redirect STDERR to STDOUT closing the STDERR filehandle).

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: redirecting standard output and standard error
by holli (Monsignor) on Sep 04, 2006 at 13:21 UTC
    IPC::Open2, IPC::Open3.
    Though I'm still not sure what is the real difference between the two (despite some syntax stuff).


    holli, /regexed monk/
      Though I'm still not sure what is the real difference between the two (despite some syntax stuff).
      • Open2 - STDIN, STDOUT
      • Open3 - STDIN, STDOUT & STDERR

      Although I would personally use IPC::Run3 module which has a much simpler API in my opinion.

Re: redirecting standard output and standard error
by Skeeve (Priest) on Sep 04, 2006 at 13:55 UTC
    open(STDERR, '>', '/dev/null'); system("/i/dont/exist")

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: redirecting standard output and standard error
by graff (Canon) on Sep 04, 2006 at 15:06 UTC
    Skeeve has shown how to get rid of the stderr output from the system call.

    The error message is saying that when "system()" invokes a shell to run your "java -version" command, the shell is unable to find an application called "java" in its current PATH. But I don't understand the "UX:" part.

    What operating system and shell are you using? This might account for why the redirection didn't work as expected. On my bsd-based darwin OS, the first of the two commands below produces an error message on the console, but the second does not -- meanwhile, a freebsd system behaves differently:

    perl -e 'system( "foo > junk" ); # prints: # sh: line 1: foo: command not found (on darwin) # foo: not found (on freebsd 6.1) perl -e 'system( "foo > junk 2>/dev/null" ); # no output on darwin # still prints "foo: not found" on freebsd
    I'm not sure if that's a difference in how Perl was built on the two machines I happened to be using, or whether it's a matter of how the shells work.

    It may also be relevant that the ability to redirect stderr and stdout separately is only available for Bourne-style shells (sh, bash, ksh, zsh); in "c-style" shells ("csh" and any others like it), you can redirect stdout, and you can have stderr included with stdout when redirecting stdout, and that's all you can do ( 2> errlog is not supported, which is a big reason why I never use csh).

      I guess system("something 2>/dev/null") should redirect the error output of "something". Not that of the shell calling "something" So I would expect the error to be displayed when "something" isn't available (the error is produced by the shell) and to be suppressed when "something" is available, but produces an error.

      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: redirecting standard output and standard error
by OfficeLinebacker (Chaplain) on Sep 04, 2006 at 19:41 UTC
    IPC::Run is the way to go IMO.

    _________________________________________________________________________________

    I like computer programming because it's like Legos for the mind.

Login:
Password
remember me
What's my password?
Create A New User

Node Status?
node history
Node Type: perlquestion [id://571072]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
GrandFather
atcroft
duff
toma
herveus
MidLifeXis
NodeReaper
Intrepid
muba
ssandv
djp
AndyZaft
Neighbour
im2
As of 2010-09-06 03:28 GMT
Sections?
Seekers of Perl Wisdom
Cool Uses for Perl
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Perl News
See About the sections of PerlMonks
Information?
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes?
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers?
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth?

My favourite poll on PerlMonks is ...

Your first Perl Book - the first one ever
Average number of caffeinated beverages per work day - the poll with the highest participation
My Thoughts on the New Voting/Experience System - the poll with the fewest votes cast
When I grow up, I want to be: - one of the polls with the fewest options
Perl 6 will primarily be: - the first one on Perl6
When I see a poll - one of the many polls about polls
this poll ;-)
yet to come
none - I hate polls. Bah.
some other

Results (95 votes), past polls