Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How do I test if my PERL script was run using a login vs a non-login shell

by aaron_baugher (Deacon)
on May 23, 2012 at 04:14 UTC ( #971942=note: print w/ replies, xml ) Need Help??


in reply to How do I test if my Perl script was run using a login vs a non-login shell

The variable $^X contains the name of the perl executable, and perlvar says it comes from C's argv[0], so that seems like it should be the answer. It doesn't seem to include the hyphen like it does in C for some reason, though.

As an aside, on FreeBSD I do get the hyphen (in C) in a login shell and not otherwise, but I get "su" and "-su" instead of the name of the shell like you get in Linux. So the hyphen part may be portable, but it doesn't look like the rest is.

One option would be to look for an environment variable that's set in a login shell and not otherwise. Hard to say how portable that would be, though. It's pretty hard to make anything universally portable when you're talking about the shell, since there are so many different ones, even on the same OS.

Aaron B.
Available for small or large Perl jobs; see my home node.


Comment on Re: How do I test if my PERL script was run using a login vs a non-login shell
Select or Download Code
Re^2: How do I test if my Perl script was run using a login vs a non-login shell
by paulski82 (Novice) on May 23, 2012 at 04:39 UTC
    The variable $^X contains the name of the Perl executable, and perlvar says it comes from C's argv[0], so that seems like it should be the answer. It doesn't seem to include the hyphen like it does in C for some reason, though.

    I did search the perlvar man pages and noted the same as you that $^X doesn't have the starting '-'. I'm beginning to think that Perl is not the language to be writing a shell in. I could write the C code but it's way harder to manage.

    As an aside, on FreeBSD I do get the hyphen (in C) in a login shell and not otherwise, but I get "su" and "-su" instead of the name of the shell like you get in Linux. So the hyphen part may be portable, but it doesn't look like the rest is.

    As long as I can get the '-' I can fudge the rest.

    One option would be to look for an environment variable that's set in a login shell and not otherwise. Hard to say how portable that would be, though. It's pretty hard to make anything universally portable when you're talking about the shell, since there are so many different ones, even on the same OS.

    My script will be the shell so I'm not sure of any variable that's going to be reliable. Unless when Perl is called it runs under a shell itself and I can make the decision based on the shell Perl runs under. That may be good enough.

    I may have to do a hack like:

    If bash and VAR1 is set assume login OR If ksh and VAR2 is set assume login.

    But that's kinda ugly and I may just end up writing the whole shell in C.

      Unless when PERL is called it runs under a shell itself and I can make the decision based on the shell PERL runs under.

      This is somewhat off the topic, but could you please stop shouting the language's name every time you use it?

        I have updated the thread so that this is no longer the case.

      You can find out what environment variables are set for your shell in different cases by making your shell:

      #!/usr/bin/perl print "$_: $ENV{$_}\n" for keys %ENV;

      Aaron B.
      Available for small or large Perl jobs; see my home node.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://971942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-12-28 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls