Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

DNScmd in perl script

by ajd335 (Novice)
on Jul 28, 2008 at 20:48 UTC ( #700639=perlquestion: print w/replies, xml ) Need Help??
ajd335 has asked for the wisdom of the Perl Monks concerning the following question:

Hey All, I have Dnscmd command in my perl script.
$domain = ""; @zones = `dnscmd $domain /EnumZones`; if ($?) { print "Error "; else " zones done";
I wonder whether its the right way or not , Because It shows me the error message part. Thanks...

Replies are listed 'Best First'.
Re: DNScmd in perl script
by shmem (Chancellor) on Jul 28, 2008 at 21:12 UTC
    Hey All, I have Dnscmd command in my perl script.

    You have glaring errors in your script. Running it as you posted it, results in

    syntax error at - line 5, near "else" Missing right curly or square bracket at - line 6, at end of line Execution of - aborted due to compilation errors.


    _($_=" "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}
      Hi Shmen , I forget to copy the closing bracket for the If statment the code is
      $domain = ""; @zones = `dnscmd $domain /EnumZones`; if ($?) { print "Error "; } else "zones done";
        if ($?) { print "Error "; } else "zones done";

        You are still missing curlies.

Re: DNScmd in perl script
by pc88mxer (Vicar) on Jul 28, 2008 at 21:12 UTC
    Technically to only test the status code you should use:
    if ($? >> 8) { print "Error " }
    The lower eight bits of $? contain the signal that terminated the process.

    There's more info in the perldoc POSIX documentation. In particular, see the documentation for the functions WEXITSTATUS and WIFEXITED.

    Note that you shouldn't always trust the status code since it is possible for the command to terminate (like via a signal) before it has a chance to set it.

      Technically to only test the status code you should use:

      But why would he want to ignore other kinds of errors?

      Hi , Yes true thing. But the value of $? m getting is -1 all the times...
        I have a feeling that dnscmd is not in your PATH.

        Try printing $! and see if it is No such file or directory.

Re: DNScmd in perl script
by toolic (Bishop) on Jul 28, 2008 at 21:14 UTC
    Aside from the fact that the code you posted does not compile because the if/else is missing curlies, it seems as though the dnscmd command is failing. You should print the value of $? so that you can get more information as to the cause of the failure.
      Hi Toolic , Yes I forgot to copy paste the curly brackets. The value of $? gives me -1 . I have tried out the same command with same argument on DOS , which gives me correct zones.
      if (not @zones) { if ($?) { print "Error "; } else { " zones done"; } }
      So was wondering why its like that..

        I personally believe, ajd335, that you made my day: we recommend all the time not to retype code to avoid such inconveniences, and appearently you have "selective" a copy-paste which will emulate the same error! Sorry for the humour, but please do not take it as personal attack and accept it as a good piece of advice to the effect of really using copy-paste.

        If you can't understand the incipit, then please check the IPB Campaign.
Re: DNScmd in perl script
by almut (Canon) on Jul 28, 2008 at 21:21 UTC

    In addition to what's already been said, in case $? is -1, it means dnscmd couldn't be started (e.g. not found along PATH).

      Hi Almut, Yes , But if I try the
      C:\ > dnscmd servername /EnumZones
      is giving me the correct result on Dos prompt.

        Presumably you have different environments. Try specifying the full path to the program. Also, in case $? is -1 (and only then), print $! to see the system error message.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://700639]
Approved by Corion
[LanX]: Jeez ... the new location for GPW so remote that hotels are cheaper than AirBnB oO
[LanX]: ... and I can get faster to the Dutch PW or French PW ...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2018-03-19 11:50 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (239 votes). Check out past polls.