Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Problem in command line arguements

by firearm12 (Novice)
on Mar 23, 2012 at 05:58 UTC ( #961149=perlquestion: print w/replies, xml ) Need Help??
firearm12 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, I need to write a script which can check the first parameter (with which it is invoked). e.g if its invoked using # perl <> or # sudo perl <>, then I need to find some way to recognise that the first command line parameter on the shell whether it was 'perl' or 'sudo'. @ARGV only gives me arguement list and $0 gives me my script name. Is there any variable which stores the parameter passed before the actual script name.Please can anyone help me in this.

Replies are listed 'Best First'.
Re: Problem in command line arguements
by davido (Archbishop) on Mar 23, 2012 at 06:50 UTC

    XY problem?

    Do you really need to know what came on the left hand side of "perl" on the command line, or is your goal to determine whether you're running as a particular user, with certain permissions, or do you need to know if you're running with different real and effective user or group ID's?

    For what it's worth, when operating under 'sudo', the environment variable $ENV{USER} will contain the login of the person who invoked sudo, and $ENV{SUDO_USER} will contain the login of the sudo target user (typically root, unless configured for some other user or otherwise specified). Sudo does set both real and effective uid and gid to the target user/group, so it wouldn't do any good detecting a mismatch there. But by detecting the USER adn SUDO_USER mismatch, you can detect sudo's effects.

    So you could just check for the existence of $ENV{SUDO_USER}, or possibly, for $ENV{USER} and $ENV{SUDO_USER} to disagree. If either of those cases occur, you're probably running under sudo.

    ('probably', unless someone went to the trouble of tampering with those variables -- though I can't think of any reason why someone might.)


      Thanks very much, I now know what I need to do.
Re: Problem in command line arguements
by BrowserUk (Pope) on Mar 23, 2012 at 06:55 UTC
    Is there any variable which stores the parameter passed before the actual script name

    No. That information is not available anywhere within your perl script.

    When you type the command line sudo perl the following steps occur:

    1. The command shell starts the sudo program passing 'sudo' as it first argument, 'perl' as the second, '' as the third.
    2. The sudo program then starts perl passing 'perl' as it first argument and '' as the second.

    Perl is never aware of the original first argument.

    However, there are a couple of approaches you might use to discover it:

    • If your OS provides an api that allows you to screen-scrape the terminal session, that might work.

      For example, on windows you could use Win32::Console::ReadRect() to grab the text from the terminal window. You could then search it for the line containing your script name.

    • Alternatively, you could use getppid to obtain the pid of your parent process.

      With that, most OSs provide a mechanism for finding out the name of the process with that pid.

      On windows you might use Win32::Process::Info, or grep the output of the tasklist command.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Thanks very much for your response.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://961149]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2017-02-24 06:51 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (353 votes). Check out past polls.