Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Perl-Sensitive Sunglasses
 
PerlMonks  

Backticks, $?, and Sudo

by ignatz (Vicar)
on Jan 16, 2003 at 21:05 UTC ( #227502=perlquestion: print w/ replies, xml ) Need Help??
ignatz has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I've inherited an application written without strict that is chocked full of complex backtick commands including some that use sudo to run as root. While playing with this pile of spaghetti I've noticed some curious behavior about things in Perl that I don't usually think about:

#!/usr/local/bin/perl -w use strict; my @commands = ('sudo /foo/bar/paco', '/foo/bar/paco', 'ls -laF'); for (@commands) { my $return = `$_`; print "\$ $_\n"; print "ERRNO = $!\n"; print "CHILD_ERROR = $?\n"; print $return; print "\n******************************************\n\n"; }
Questions:
  • How do you get the value of $? from a command run as a different user?
  • Why does CHILD_ERROR = Illegal seek return for successful commands?
  • Is there any point to trying to refactor this style of coding, or is it better to start over from scratch?
Just curious...
()-()
 \"/
  `

Comment on Backticks, $?, and Sudo
Download Code
Re: Backticks, $?, and Sudo
by sauoq (Abbot) on Jan 17, 2003 at 04:02 UTC

    Answers:

    • Sudo should return the exit value of the command that was run.

      $ sudo perl -e 'exit 3' $ echo $? 3
      A better example might be:
      $ perl -le '`sudo perl -e "exit 3"`; print $?>>8' 3

    • Are you sure that is CHILD_ERROR you are looking at and not ERRNO? The reason I ask is twofold. The first is that $? should be numeric. The second is that I've often seen $! being set to "Illegal Seek" after a successful backtick command. I think it has to do with how perl implements the backticks. (Specifically, I think it attempts to do a seek on the pipe. I have no idea why though.) In any case, it doesn't much matter because $! isn't really applicable unless the return value extracted from $? is -1.

    • That depends...

    -sauoq
    "My two cents aren't worth a dime.";
    
      Thanks for everyone's response. helgi response makes a lot of sense. I keep forgetting that they are magic variables with super powers.

      I guess that the thing that I find really puzzling is how $! and $? act completely different when sudo is thrown into the mix:

      $> perl sudotest.pl Password: sudo: /foo/bar/paco: command not found $ sudo /foo/bar/paco ERRNO = CHILD_ERROR = 256 ****************************************** Can't exec "/foo/bar/paco": No such file or directory at sudotest.pl l +ine 8. $ /foo/bar/paco ERRNO = No such file or directory CHILD_ERROR = -1 Use of uninitialized value in print at sudotest.pl line 12. ****************************************** $ ls -laF ERRNO = Illegal seek CHILD_ERROR = 0 total 138 drwxrwxrwx 5 ignatz paco 1024 Jan 16 15:16 ./ drwxr-xr-x 21 ignatz paco 1536 Jan 16 15:34 ../ drwxrwxrwx 2 ignatz paco 512 Dec 18 15:30 CVS/ -rw-r--r-- 1 ignatz paco 298 Jan 16 14:44 sudotest.pl ******************************************
      I should have included this in the first place.
      ()-()
       \"/
        `
      

        Uhm... $! and $? do not act differently with sudo thrown in the mix. The only real difference is that you are executing a different command (i.e. sudo rather than the command that sudo is executing.)

        $> perl sudotest.pl Password: sudo: /foo/bar/paco: command not found $ sudo /foo/bar/paco ERRNO = CHILD_ERROR = 256

        In that case, sudo executed just fine and returned a 1 (which is what sudo does when the command it is given can't be executed.) Remember to shift $?. (256 >> 8 == 1)

        Can't exec "/foo/bar/paco": No such file or directory at sudotest.pl l +ine 8. $ /foo/bar/paco ERRNO = No such file or directory CHILD_ERROR = -1 Use of uninitialized value in print at sudotest.pl line 12.

        Again, normal behavior. Perl can't execute the command given so it sets $? to -1 and returns the reason in $! (as documented.)

        $ ls -laF ERRNO = Illegal seek CHILD_ERROR = 0 total 138 drwxrwxrwx 5 ignatz paco 1024 Jan 16 15:16 ./ drwxr-xr-x 21 ignatz paco 1536 Jan 16 15:34 ../ drwxrwxrwx 2 ignatz paco 512 Dec 18 15:30 CVS/ -rw-r--r-- 1 ignatz paco 298 Jan 16 14:44 sudotest.pl

        Once again, this behaved as expected. The command executed successfully, so $? contains 0. In this case, $! is not relevant.

        Keep in mind that $! is only meaningful in association with backticks and system when $? == -1.

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Backticks, $?, and Sudo
by dug (Chaplain) on Jan 17, 2003 at 04:48 UTC
    How do you get the value of $? from a command run as a different user?

    sudo's documentation (at least my sudo's documentation) says that it returns the return value of the program it executes. All my tests indicate that it does what it purports.


    Why does CHILD_ERROR = Illegal seek return for successful commands?

    I think you mean $ERRNO. But I have no clue :-) Backticks and qx() do some work to gather the output of the command executed, and only promise to set $? with the status of the command executed. I guess it's possible that they do some funky busines that sets $! in the process, but again, I sure don't know.


    Is there any point to trying to refactor this style of coding, or is it better to start over from scratch?

    I sure don't mind shelling out work when it's appropriate.

    A couple of reccomendations (which you have probably already thought about):

    In places where you don't care about the output of the commands, use system() instead of backticks, since it doesn't suffer from the overhead of capturing the command's output.

    If you find yourself doing a bunch of parsing of command ouput, someone may have written a module for it. For example, instead of doing filter_results( `ps -elf` );, use Proc::ProcessTable.

    I'll be very curious to hear the answer to your second question. In the mean time, good luck!

    -- dug
Re: Backticks, $?, and Sudo
by helgi (Hermit) on Jan 17, 2003 at 11:03 UTC
    For backticks and qx you return the error via $! and not $?

    Thus $? is Illegal because you haven't run a child process and thus get no CHILD_ERROR.

    --
    Regards,
    Helgi Briem
    helgi AT decode DOT is

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://227502]
Approved by jwest
Front-paged by fsn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-04-19 15:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (482 votes), past polls