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

Output redirection -- hard

by dorp (Novice)
on Aug 07, 2001 at 03:23 UTC ( [id://102649]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have a program "cross_match" on my system, which parses a DNA file. It dumps a ton of text garbage on the screen. I have tried all the standard ways of redirecting output:
system("cross_match -arguments > /dev/null"); # dumps garbage anyway `cross_match`; # dumps garbage anyway system("cross_match -arguments 2>&1"); # won't work because cross_matc +h manages to interpret 2>&1 as an invalid argument system("cross_match -arguments > log.txt 2>&1"); # won't work because +of "ambiguous redirect" open("cross_match -arguments |"); #won't work because cross_match mana +ges to interpret | as an invalid argument open2(\*REALIN, \*REALOUT, "cross_match -arguments"); # crashes, says +arguments have to be passed on the command line
For some strange reason, no matter what I've tried, it still manages to dump a ton of garbage on the screen. Does anyone know of another way?

Replies are listed 'Best First'.
Re: Output redirection -- hard
by ariels (Curate) on Aug 07, 2001 at 15:29 UTC
    "Ambiguous output redirect" is the message that csh emits when you say "cmd >/tmp/foo 2>&1". Somehow your Perl is using /bin/csh (instead of /bin/sh) to run your program.

    In system("cross_match -arguments 2>&1"), what probably happens is that you're running "cross_match -arguments 2", redirecting standard output and standard error to a file named "2"; this is /bin/csh behaviour.

    Does the exact code that you posted exhibit this behaviour?

    Please try running "perl -e 'print system(q(ps x | grep $$))'" and posting the result; this will tell us definitely which shell is running...

Re: Output redirection -- hard
by bikeNomad (Priest) on Aug 07, 2001 at 08:14 UTC
    I assume you have a unix system, or the /dev/null reference wouldn't work (you don't say).

    Before you spend much more time in Perl, can you get this program to work quietly from the shell? I can't see why your program would interpret '2>&1' or '|' as arguments, or why you get the 'ambiguous redirect' error. Are you sure that your Perl is using a standard (i.e. non-csh) shell? Perl should call something like '/bin/sh -c', which should then eat the redirection characters.

    I'd try to run this program from the shell first, and then use the same command line for perl's system command (assuming you don't have to feed the program anything via a pipe or something). If that fails, perhaps Perl is using the wrong shell.

    If the program is doing something dumb like talking to /dev/tty, you might have to use something more industrial-strength to talk to this program, like using /dev/pty*, etc.

Re: Output redirection -- hard
by mischief (Hermit) on Aug 07, 2001 at 14:44 UTC

    It's pretty easy - all you have to do is open STDERR as a filehandle, directed to /dev/null:

    #!/usr/bin/perl -w use strict; open (STDERR, "/dev/null"); select STDERR; print "hi mum!";

    for example.

    You might also want to read perlfaq8, including "How can I capture STDERR from an external command?", or type "perldoc -q STDERR".

Re: Output redirection -- hard
by ws_stefan (Initiate) on Aug 07, 2001 at 10:28 UTC
    i've had a lot of problems handling output when using the system command so normally i'll just use the back ticks to execute a command from the command line.

    for example

    $string = `command 2>/dev/null`;

    puts the output of the command into string and the error code into /dev/null

    or simply `command >/dev/null`;

    *notice* - those are backticks, not apostrophe's
      Additional you can use $? to get the error code from your backtics command.
      ----------------------------------- --the good, the bad and the physi-- -----------------------------------

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found