Re: login shell?



On 7/27/05, Bryan R Harris <Bryan_R_Harris@xxxxxxxxxxxx> wrote:
>
>
> Is there a way to determine in perl whether the current script was run from
> a login shell or not?
>
> - B
>
>

that depends what you mean by "login shell". You can use $ENV{SHELL}
to find out what shell, if any, invoked the the current process. You
can also test if the program has been invoked interactively:

if ( -t STDIN && STDOUT) {
we're probably interactive
}

That will return false if the job is being run via cron, but it will
also return false if there is command line redirection.

The Perl Cookbook has a slightly more complex example in recipe 15.2:

use POSIX qw/getpgrp tcgetpgrp/;

sub am_I_intereactive {
my $tty;
open($tty, "<", "/dev/tty")
or die "can't open /dev/tty: $!";
my $tpgrp = tcgetpgrp(fileno($tty));
my $pgrp = getpgrp();
close $tty;
return ($tpgrp == $pgrp);
}

This will tell you if you have control of a tty, which means the
program was probably invoked from an interactive session, even with
redirection in place.

What none of this will tell you is whether this is a "login" shell.
Just because a shell is interactive, that doesn't mean it's a login
shell. a user can change his/her shell at the command line by typing
/bin/shell, or using su. In order to really be sure that you were in a
login shell, you would need to check for interactivity, and then check
that the parent of the shell that invoked perl was the appropriate
daemon on your system. And then just for good measure, you'd probably
want to check the result against the contents of /etc/shell or the
equivalent on your system to make sure that the shell is a valid login
shell, and then against the contents of /etc/passwd or equivalent to
make sure that it is the specified login shell for the current user.

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [ x ] blogable; [ ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org
.