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
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}
Though I'm still not sure what is the real difference between the two (despite some syntax stuff).
Although I would personally use IPC::Run3 module which has a much simpler API in my opinion.
open(STDERR, '>', '/dev/null');
system("/i/dont/exist")
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 freebsdI'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 like computer programming because it's like Legos for the mind.
perlmonks.org content © perlmonks.org and adrianh, graff, holli, OfficeLinebacker, s_gaurav1091, shmem, Skeeve
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03