Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

capturing error messages from backticks or system

by Piercer (Beadle)
on Aug 28, 2001 at 18:53 UTC ( [id://108461]=perlquestion: print w/replies, xml ) Need Help??

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

I'm running a command on loads of NT boxes to map a drive. Sometimes it fails. Actually loads of times it fails (I love NT) for lots of different reasons - I'm trying to get the error produced so I can fix them but having loads of difficulty. I wrote the following test to test what error is produces
undef $^E; undef $!; undef $?; print "using system \n"; system("net use v: \\\\SERVERNAME\\SHARENAME"); print "dollar caret E reports $^E \n"; print "dollar Exclamation mark reports $! \n"; print "dollar question mark reports $? \n"; print "using backticks \n"; `net use v: \\\\SERVERNAME\\SHARENAME`; print "dollar caret E reports $^E \n"; print "dollar Exclamation mark reports $! \n"; print "dollar question mark reports $? \n";
What I get is this (I'v formatted it a bit to make it a little more readable.)
using system System error 85 has occurred. The local device name is already in use. dollar caret E reports The system cannot find the file specif +ied dollar Exclamation mark reports dollar question mark reports 512 using backticks System error 85 has occurred. The local device name is already in use. dollar caret E reports The pipe has been ended dollar Exclamation mark reports dollar question mark reports 512
What I really want is the english readable message eg System error 85 has occurred.The local device name is already in use. I think this is coded somehow in $? but my brain hurts and I can't figure it out. Has anyone any hints, tricks or a suggestion of where I should be looking for the answer?

Replies are listed 'Best First'.
Re: capturing error messages from backticks or system
by grinder (Bishop) on Aug 28, 2001 at 20:31 UTC
    Rather than calling a flakey external program, why not just get Perl to do the job itself? Use Win32::NetResource and NetShareAdd, and then in essence all you're doing is performing the underlying OS call directly. Should be much faster and less error-prone.

    --
    g r i n d e r
Re: capturing error messages from backticks or system
by Hofmator (Curate) on Aug 28, 2001 at 19:53 UTC

    What about capturing the output of the command you are calling? And probably you have to redirect STDERR because this captures only STDOUT ... for the redirection you have to write 2>&1 iirc (sorry, no NT box around at the moment). So the code to use is: my $msg = `net use v: \\\\SERVERNAME\\SHARENAME 2>&1`;

    -- Hofmator

Re: capturing error messages from backticks or system
by rchiav (Deacon) on Aug 28, 2001 at 19:42 UTC
    I think that your problem is that the "english readable" error isn't being produced by "net". It's comming from someplace else (the OS). If you redirect STDERR, you'll see that STDERR from the call to net is not the "english readable" message. I'm testing this on 2000, so I can't verify the results on NT. Here's my results.
    #!/usr/bin/perl -w use strict; my $results = `net use f: \\\\SERVER\\c\$ 2>&1`; print "results contains - $results\n";
    results -
    C:\dev>map.pl results contains - System error 1202 has occurred. An attempt was made to remember a device that had previously been reme +mbered.
    On NT, your whole error message may be in STDERR. It's not in 2000 though.

    Hope this helps..
    Rich

    update: I'm an idiot. The entire message is in $results. So forget I mentioned that the whole thing wasn't sent to STDERR. I assumed it wasn't because of the new lines. I guess that's what I get for thinking! :)

      Your test code isn't conclusive. Since you don't have a clear ending delimiter in your print statement, we can't tell whether the "An attempt..." string is part of $results or not.

      Fixing that:

      #!/usr/bin/perl -w use strict; my $results = `net use f: \\\\SERVER\\c\$ 2>&1`; print "results contains ($results)\n";
      gives me this:
      results contains (System error 67 has occurred. The network name cannot be found. )
      on Win2K. So the error message is going to STDERR, even in Win2K.

      For capturing STDERR, IPC::Open2 and IPC::Open3 are often good alternatives.

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

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

    No recent polls found