Home > Archive > PERL Beginners > July 2005 > login shell?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Bryan R Harris 2005-07-27, 5:02 pm |
|
Is there a way to determine in perl whether the current script was run from
a login shell or not?
- B
| |
| Jeff 'japhy' Pinyan 2005-07-27, 5:02 pm |
| On Jul 27, Bryan R Harris said:
> Is there a way to determine in perl whether the current script was run from
> a login shell or not?
As opposed to what, specifically? I'd say a good place to check is
$ENV{SHELL}. If that's defined, chances are it was run from a shell. I
would not expect it to be set from a web browser.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
| |
| Jay Savage 2005-07-27, 5:02 pm |
| On 7/27/05, Bryan R Harris <Bryan_R_Harris@raytheon.com> wrote:
>=20
>=20
> Is there a way to determine in perl whether the current script was run fr=
om
> a login shell or not?
>=20
> - B
>=20
>=20
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 =3D tcgetpgrp(fileno($tty));
my $pgrp =3D getpgrp();
close $tty;
return ($tpgrp =3D=3D $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
| |
| Bryan R Harris 2005-07-27, 5:02 pm |
|
Thanks for the tips, guys.
The %ENV hash suggestion worked -- I'm actually writing a filter for BBEdit
(OS X) that I also want to work from the command line, and also on IRIX
boxes. Turns out that all CLIs have $ENV{TERM} defined, but within BBEdit
it's not.
But turns out it doesn't matter anyway! I thought BBEdit required "\r"
linebreaks internally, but apparently it translates the "\n"s it gets from
unix filters, so it doesn't matter.
Thanks again.
- B
> On 7/27/05, Bryan R Harris <Bryan_R_Harris@raytheon.com> wrote:
>
> 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
| |
| JupiterHost.Net 2005-07-27, 10:00 pm |
|
Bryan R Harris wrote:
>
> Is there a way to determine in perl whether the current script was run from
> a login shell or not?
if(-t STDIN) {
print "Hello terminal\n";
} else {
print "Content-type: text/html\n\n";
print "Hello <b>Browser</b>\n";
}
HTH :)
| |
| Jay Savage 2005-07-28, 9:59 pm |
| On 7/27/05, JupiterHost.Net <mlists@jupiterhost.net> wrote:
>=20
>=20
> Bryan R Harris wrote:
from[color=darkred]
>=20
> if(-t STDIN) {
> print "Hello terminal\n";
> } else {
> print "Content-type: text/html\n\n";
> print "Hello <b>Browser</b>\n";
> }
>=20
> HTH :)
That doesn't do what you probably want it to do; see my comments
above. Or just consider the following:
~/perl> perl -e 'if (-t STDIN){print "terminal\n";}else{print "html\n";}'
terminal
~/perl> perl -e 'if (-t STDIN){print "terminal\n";}else{print
"html\n";}' < some.file
html
You don't even have to read from <>, there just has to be command line
redirection
HTH,
-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential
daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org
| |
| Tom Allison 2005-07-29, 10:00 pm |
| Bryan R Harris wrote:
>
> Is there a way to determine in perl whether the current script was run from
> a login shell or not?
>
> - B
>
>
>
ENV{PS1} ???
That's mentioned in my .bashrc script
# If running interactively, then:
if [ "$PS1" ]; then
so I assume that it's part of the %ENV and if so defined....
| |
| John W. Krahn 2005-07-29, 10:00 pm |
| Tom Allison wrote:
> Bryan R Harris wrote:
>
> ENV{PS1} ???
>
> That's mentioned in my .bashrc script
>
>
> # If running interactively, then:
> if [ "$PS1" ]; then
>
>
> so I assume that it's part of the %ENV and if so defined....
~> echo "*$PS1*"
*\w> *
~> perl -le'print "*$ENV{PS1}*"'
**
Never assume ...
:-)
John
--
use Perl;
program
fulfillment
|
|
|
|
|