In summary, I need a portable way to test from inside my Perl script if the script was run using a login (su - <username>) vs non-login (su <username>) where the script is the shell of the user I'm trying to su to.

I can do this easily in C using the following code:

/* testsh2.c */ #include <stdlib.h> #include <stdio.h> int main(int argc, char** argv) { int i = 0; for (;i < argc; i++) { printf("argv[%d]: %s\n", i, argv[i]); } exit(0); }

Password file is as follows (this is a Red Hat Enterpise Linux 5 server). The /tmp/testsh2 is the C code above compiled.

root@aster /tmp# cat /etc/redhat-release Red Hat Enterprise Linux Server release 5.8 (Tikanga) root@aster /tmp# cat /etc/passwd| grep paultest paultest:x:401:401::/home/paultest:/tmp/testsh2

Here's the output when I run the C program.

root@aster /tmp# su - paultest argv[0]: -testsh2 root@aster /tmp# su paultest argv[0]: testsh2

As you can see, I can just test if the first character argv[0] is a '-'.

I'm having trouble doing the same under Perl.

The variable $0 doesn't have a '-' at the start of it under Perl and the ARGV array doesn't have the program name as the first element like in C.

Is there any way to get this information under Perl?

NOTE: I have searched the archives for things like 'login shell', but there are too many hits, as you'd expect.


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.