Shell-Guess
view release on metacpan or search on metacpan
lib/Shell/Guess.pm view on Meta::CPAN
sub name
{
my $self = ref $_[0] ? shift : __PACKAGE__->running_shell;
$self->{name};
}
sub default_location
{
my $self = ref $_[0] ? shift : __PACKAGE__->running_shell;
$self->{default_location};
}
sub _unixy_shells
{
my $shell = shift;
if($shell =~ /tcsh$/)
{ return __PACKAGE__->tc_shell }
elsif($shell =~ /csh$/)
{ return __PACKAGE__->c_shell }
elsif($shell =~ /ksh$/)
{ return __PACKAGE__->korn_shell }
elsif($shell =~ /bash$/)
{ return __PACKAGE__->bash_shell }
elsif($shell =~ /zsh$/)
{ return __PACKAGE__->z_shell }
elsif($shell =~ /fish$/)
{ return __PACKAGE__->fish_shell }
elsif($shell =~ /pwsh$/)
{ return __PACKAGE__->power_shell }
elsif($shell =~ /sh$/)
{ return __PACKAGE__->bourne_shell }
else
{ return; }
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Shell::Guess - Make an educated guess about the shell in use
=head1 VERSION
version 0.10
=head1 SYNOPSIS
guessing shell which called the Perl script:
use Shell::Guess;
my $shell = Shell::Guess->running_shell;
if($shell->is_c) {
print "setenv FOO bar\n";
} elsif($shell->is_bourne) {
print "export FOO=bar\n";
}
guessing the current user's login shell:
use Shell::Guess;
my $shell = Shell::Guess->login_shell;
print $shell->name, "\n";
guessing an arbitrary user's login shell:
use Shell::Guess;
my $shell = Shell::Guess->login_shell('bob');
print $shell->name, "\n";
=head1 DESCRIPTION
Shell::Guess makes a reasonably aggressive attempt to determine the
shell being employed by the user, either the shell that executed the
perl script directly (the "running" shell), or the users' login shell
(the "login" shell). It does this by a variety of means available to
it, depending on the platform that it is running on.
=over 4
=item * getpwent
On UNIXy systems with getpwent, that can be used to determine the login
shell.
=item * dscl
Under Mac OS X getpwent will typically not provide any useful information,
so the dscl command is used instead.
=item * proc file systems
On UNIXy systems with a proc filesystems (such as Linux), Shell::Guess
will attempt to use that to determine the running shell.
=item * ps
On UNIXy systems without a proc filesystem, Shell::Guess will use the
ps command to determine the running shell.
=item * L<Win32::Getppid> and L<Win32::Process::List>
On Windows if these modules are installed they will be used to determine
the running shell. This method can differentiate between PowerShell,
C<command.com> and C<cmd.exe>.
=item * ComSpec
If the above method is inconclusive, the ComSpec environment variable
will be consulted to differentiate between C<command.com> or C<cmd.exe>
(PowerShell cannot be detected in this manner).
=item * reasonable defaults
If the running or login shell cannot be otherwise determined, a reasonable
default for your platform will be used as a fallback. Under OpenVMS this is
dcl, Windows 95/98 and MS-DOS this is command.com and Windows NT/2000/XP/Vista/7
this is cmd.exe. UNIXy platforms fallback to bourne shell.
=back
The intended use of this module is to enable a Perl developer to write
a script that generates shell configurations for the calling shell so they
can be imported back into the calling shell using C<eval> and backticks
or C<source>. For example, if your script looks like this:
#!/usr/bin/perl
use Shell::Guess;
my $shell = Shell::Guess->running_shell;
if($shell->is_bourne) {
print "export FOO=bar\n";
} else($shell->is_c) {
print "setenv FOO bar\n";
} else {
die "I don't support ", $shell->name, " shell";
}
You can then import FOO into your bash or c shell like this:
% eval `perl script.pl`
or, you can write the output to a configuration file and source it:
% perl script.pl > foo.sh
% source foo.sh
L<Shell::Config::Generate> provides a portable interface for generating
such shell configurations, and is designed to work with this module.
=head1 CLASS METHODS
These class methods return an instance of Shell::Guess, which can then be
interrogated by the instance methods in the next section below.
=head2 running_shell
my $shell = Shell::Guess->running_shell;
Returns an instance of Shell::Guess based on the shell which directly
started the current Perl script. If the running shell cannot be determined,
it will return the login shell.
=head2 login_shell
my $shell = Shell::Guess->login_shell;
my $shell = Shell::Guess->login_shell( $username )
Returns an instance of Shell::Guess for the given user. If no username is specified then
the current user will be used. If no shell can be guessed then a reasonable fallback
will be chosen based on your platform.
=head2 bash_shell
my $shell = Shell::Guess->bash_shell;
Returns an instance of Shell::Guess for bash.
The following instance methods will return:
=over 4
=item * $shell-E<gt>name = bash
=item * $shell-E<gt>is_bash = 1
=item * $shell-E<gt>is_bourne = 1
=item * $shell-E<gt>is_unix = 1
=item * $shell-E<gt>default_location = /bin/bash
=back
( run in 1.064 second using v1.01-cache-2.11-cpan-df04353d9ac )