.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Shell::Guess 3pm" .TH Shell::Guess 3pm "2018-12-09" "perl v5.28.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Shell::Guess \- Make an educated guess about the shell in use .SH "VERSION" .IX Header "VERSION" version 0.09 .SH "SYNOPSIS" .IX Header "SYNOPSIS" guessing shell which called the Perl script: .PP .Vb 7 \& use Shell::Guess; \& my $shell = Shell::Guess\->running_shell; \& if($shell\->is_c) { \& print "setenv FOO bar\en"; \& } elsif($shell\->is_bourne) { \& print "export FOO=bar\en"; \& } .Ve .PP guessing the current user's login shell: .PP .Vb 3 \& use Shell::Guess; \& my $shell = Shell::Guess\->login_shell; \& print $shell\->name, "\en"; .Ve .PP guessing an arbitrary user's login shell: .PP .Vb 3 \& use Shell::Guess; \& my $shell = Shell::Guess\->login_shell(\*(Aqbob\*(Aq); \& print $shell\->name, "\en"; .Ve .SH "DESCRIPTION" .IX Header "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 \*(L"running\*(R" shell), or the users' login shell (the \*(L"login\*(R" shell). It does this by a variety of means available to it, depending on the platform that it is running on. .IP "\(bu" 4 getpwent .Sp On UNIXy systems with getpwent, that can be used to determine the login shell. .IP "\(bu" 4 dscl .Sp Under Mac \s-1OS X\s0 getpwent will typically not provide any useful information, so the dscl command is used instead. .IP "\(bu" 4 proc file systems .Sp On UNIXy systems with a proc filesystems (such as Linux), Shell::Guess will attempt to use that to determine the running shell. .IP "\(bu" 4 ps .Sp On UNIXy systems without a proc filesystem, Shell::Guess will use the ps command to determine the running shell. .IP "\(bu" 4 Win32::Getppid and Win32::Process::List .Sp On Windows if these modules are installed they will be used to determine the running shell. This method can differentiate between PowerShell, \&\f(CW\*(C`command.com\*(C'\fR and \f(CW\*(C`cmd.exe\*(C'\fR. .IP "\(bu" 4 ComSpec .Sp If the above method is inconclusive, the ComSpec environment variable will be consulted to differentiate between \f(CW\*(C`command.com\*(C'\fR or \f(CW\*(C`cmd.exe\*(C'\fR (PowerShell cannot be detected in this manner). .IP "\(bu" 4 reasonable defaults .Sp 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. .PP 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 \f(CW\*(C`eval\*(C'\fR and backticks or \f(CW\*(C`source\*(C'\fR. For example, if your script looks like this: .PP .Vb 10 \& #!/usr/bin/perl \& use Shell::Guess; \& my $shell = Shell::Guess\->running_shell; \& if($shell\->is_bourne) { \& print "export FOO=bar\en"; \& } else($shell\->is_c) { \& print "setenv FOO bar\en"; \& } else { \& die "I don\*(Aqt support ", $shell\->name, " shell"; \& } .Ve .PP You can then import \s-1FOO\s0 into your bash or c shell like this: .PP .Vb 1 \& % eval \`perl script.pl\` .Ve .PP or, you can write the output to a configuration file and source it: .PP .Vb 2 \& % perl script.pl > foo.sh \& % source foo.sh .Ve .PP Shell::Config::Generate provides a portable interface for generating such shell configurations, and is designed to work with this module. .SH "CLASS METHODS" .IX Header "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. .SS "running_shell" .IX Subsection "running_shell" .Vb 1 \& my $shell = Shell::Guess\->running_shell; .Ve .PP 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. .SS "login_shell" .IX Subsection "login_shell" .Vb 2 \& my $shell = Shell::Guess\->login_shell; \& my $shell = Shell::Guess\->login_shell( $username ) .Ve .PP 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. .SS "bash_shell" .IX Subsection "bash_shell" .Vb 1 \& my $shell = Shell::Guess\->bash_shell; .Ve .PP Returns an instance of Shell::Guess for bash. .PP The following instance methods will return: .IP "\(bu" 4 \&\f(CW$shell\fR\->name = bash .IP "\(bu" 4 \&\f(CW$shell\fR\->is_bash = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_bourne = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_unix = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->default_location = /bin/bash .PP All other instance methods will return false .SS "bourne_shell" .IX Subsection "bourne_shell" .Vb 1 \& my $shell = Shell::Guess\->bourne_shell; .Ve .PP Returns an instance of Shell::Guess for the bourne shell. .PP The following instance methods will return: .IP "\(bu" 4 \&\f(CW$shell\fR\->name = bourne .IP "\(bu" 4 \&\f(CW$shell\fR\->is_bourne = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_unix = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->default_location = /bin/sh .PP All other instance methods will return false .SS "c_shell" .IX Subsection "c_shell" .Vb 1 \& my $shell = Shell::Guess\->c_shell; .Ve .PP Returns an instance of Shell::Guess for c shell. .PP The following instance methods will return: .IP "\(bu" 4 \&\f(CW$shell\fR\->name = c .IP "\(bu" 4 \&\f(CW$shell\fR\->is_c = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_unix = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->default_location = /bin/csh .PP All other instance methods will return false .SS "cmd_shell" .IX Subsection "cmd_shell" .Vb 1 \& my $shell = Shell::Guess\->cmd_shell; .Ve .PP Returns an instance of Shell::Guess for the Windows \s-1NT\s0 cmd shell (cmd.exe). .PP The following instance methods will return: .IP "\(bu" 4 \&\f(CW$shell\fR\->name = cmd .IP "\(bu" 4 \&\f(CW$shell\fR\->is_cmd = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_win32 = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->default_location = C:\eWindows\esystem32\ecmd.exe .PP All other instance methods will return false .SS "command_shell" .IX Subsection "command_shell" .Vb 1 \& my $shell = Shell::Guess\->command_shell; .Ve .PP Returns an instance of Shell::Guess for the Windows 95 command shell (command.com). .PP The following instance methods will return: .IP "\(bu" 4 \&\f(CW$shell\fR\->name = command .IP "\(bu" 4 \&\f(CW$shell\fR\->is_command = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_win32 = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->default_location = C:\eWindows\esystem32\ecommand.com .PP All other instance methods will return false .SS "dcl_shell" .IX Subsection "dcl_shell" .Vb 1 \& my $shell = Shell::Guess\->dcl_shell; .Ve .PP Returns an instance of Shell::Guess for the OpenVMS dcl shell. .PP The following instance methods will return: .IP "\(bu" 4 \&\f(CW$shell\fR\->name = dcl .IP "\(bu" 4 \&\f(CW$shell\fR\->is_dcl = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_vms = 1 .PP All other instance methods will return false .SS "fish_shell" .IX Subsection "fish_shell" .Vb 1 \& my $shell = Shell::Guess\->fish_shell; .Ve .PP Returns an instance of Shell::Guess for the fish shell. .PP The following instance methods will return: .IP "\(bu" 4 \&\f(CW$shell\fR\->name = fish .IP "\(bu" 4 \&\f(CW$shell\fR\->is_fish = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_unix = 1 .SS "korn_shell" .IX Subsection "korn_shell" .Vb 1 \& my $shell = Shell::Guess\->korn_shell; .Ve .PP Returns an instance of Shell::Guess for the korn shell. .PP The following instance methods will return: .IP "\(bu" 4 \&\f(CW$shell\fR\->name = korn .IP "\(bu" 4 \&\f(CW$shell\fR\->is_korn = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_bourne = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_unix = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->default_location = /bin/ksh .PP All other instance methods will return false .SS "power_shell" .IX Subsection "power_shell" .Vb 1 \& my $shell = Shell::Guess\->power_shell; .Ve .PP Returns an instance of Shell::Guess for Microsoft PowerShell (either for Windows \f(CW\*(C`powershell.exe\*(C'\fR or Unix \f(CW\*(C`pwsh\*(C'\fR). .PP The following instance methods will return: .IP "\(bu" 4 \&\f(CW$shell\fR\->name = power .IP "\(bu" 4 \&\f(CW$shell\fR\->is_power = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_win32 = 1 .PP All other instance methods will return false .SS "tc_shell" .IX Subsection "tc_shell" .Vb 1 \& my $shell = Shell::Guess\->tc_shell; .Ve .PP Returns an instance of Shell::Guess for tcsh. .PP The following instance methods will return: .IP "\(bu" 4 \&\f(CW$shell\fR\->name = tc .IP "\(bu" 4 \&\f(CW$shell\fR\->is_tc = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_c = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_unix = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->default_location = /bin/tcsh .PP All other instance methods will return false .SS "z_shell" .IX Subsection "z_shell" .Vb 1 \& my $shell = Shell::Guess\->z_shell .Ve .PP Returns an instance of Shell::Guess for zsh. .PP The following instance methods will return: .IP "\(bu" 4 \&\f(CW$shell\fR\->name = z .IP "\(bu" 4 \&\f(CW$shell\fR\->is_z = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_bourne = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->is_unix = 1 .IP "\(bu" 4 \&\f(CW$shell\fR\->default_location = /bin/zsh .PP All other instance methods will return false .SH "INSTANCE METHODS" .IX Header "INSTANCE METHODS" The normal way to call these is by calling them on the result of either \&\fIrunning_shell\fR or \fIlogin_shell\fR, but they can also be called as class methods, in which case the currently running shell will be used, so .PP .Vb 1 \& Shell::Guess\->is_bourne .Ve .PP is the same as .PP .Vb 1 \& Shell::Guess\->running_shell\->is_bourne .Ve .SS "is_bash" .IX Subsection "is_bash" .Vb 1 \& my $bool = $shell\->is_bash; .Ve .PP Returns true if the shell is bash. .SS "is_bourne" .IX Subsection "is_bourne" .Vb 1 \& my $bool = $shell\->is_bourne; .Ve .PP Returns true if the shell is the bourne shell, or a shell which supports bourne syntax (e.g. bash or korn). .SS "is_c" .IX Subsection "is_c" .Vb 1 \& my $bool = $shell\->is_c; .Ve .PP Returns true if the shell is csh, or a shell which supports csh syntax (e.g. tcsh). .SS "is_cmd" .IX Subsection "is_cmd" .Vb 1 \& my $bool = $shell\->is_cmd; .Ve .PP Returns true if the shell is the Windows command.com shell. .SS "is_command" .IX Subsection "is_command" .Vb 1 \& my $bool = $shell\->is_command; .Ve .PP Returns true if the shell is the Windows cmd.com shell. .SS "is_dcl" .IX Subsection "is_dcl" .Vb 1 \& my $bool = $shell\->is_dcl; .Ve .PP Returns true if the shell is the OpenVMS dcl shell. .SS "is_fish" .IX Subsection "is_fish" .Vb 1 \& my $bool = $shell\->is_fish; .Ve .PP Returns true if the shell is Fish shell. .SS "is_korn" .IX Subsection "is_korn" .Vb 1 \& my $bool = $shell\->is_korn; .Ve .PP Returns true if the shell is the korn shell. .SS "is_power" .IX Subsection "is_power" .Vb 1 \& my $bool = $shell\->is_power; .Ve .PP Returns true if the shell is Windows PowerShell. .SS "is_tc" .IX Subsection "is_tc" .Vb 1 \& my $bool = $shell\->is_tc; .Ve .PP Returns true if the shell is tcsh. .SS "is_unix" .IX Subsection "is_unix" .Vb 1 \& my $bool = $shell\->is_unix; .Ve .PP Returns true if the shell is traditionally a \s-1UNIX\s0 shell (e.g. bourne, bash, korn) .SS "is_vms" .IX Subsection "is_vms" .Vb 1 \& my $bool = $shell\->is_vms; .Ve .PP Returns true if the shell is traditionally an OpenVMS shell (e.g. dcl) .SS "is_win32" .IX Subsection "is_win32" .Vb 1 \& my $bool = $shell\->is_win32; .Ve .PP Returns true if the shell is traditionally a Windows shell (command.com, cmd.exe, powershell.exe, pwsh) .SS "is_z" .IX Subsection "is_z" .Vb 1 \& my $bool = $shell\->is_z; .Ve .PP Returns true if the shell is zsh .SS "name" .IX Subsection "name" .Vb 1 \& my $name = $shell\->name; .Ve .PP Returns the name of the shell. .SS "default_location" .IX Subsection "default_location" .Vb 1 \& my $location = $shell\->default_location; .Ve .PP The usual location for this shell, for example /bin/sh for bourne shell and /bin/csh for c shell. May not be defined for all shells. .SH "CAVEATS" .IX Header "CAVEATS" Shell::Guess shouldn't ever die or crash, instead it will attempt to make a guess or use a fallback about either the login or running shell even on unsupported operating systems. The fallback is the most common shell on the particular platform that you are using, so on UNIXy platforms the fallback is bourne, and on OpenVMS the fallback is dcl. .PP These are the operating systems that have been tested in development and are most likely to guess reliably. .IP "\(bu" 4 Linux .IP "\(bu" 4 Cygwin .IP "\(bu" 4 FreeBSD .IP "\(bu" 4 Mac \s-1OS X\s0 .IP "\(bu" 4 Windows (Strawberry Perl) .IP "\(bu" 4 Solaris (x86) .IP "\(bu" 4 MS-DOS (djgpp) .IP "\(bu" 4 OpenVMS .Sp Always detected as dcl (a more nuanced view of OpenVMS is probably possible, patches welcome). .PP UNIXy platforms without a proc filesystem will use Unix::Process if installed, which will execute ps to determine the running shell. .PP It is pretty easy to fool the \->running_shell method by using fork, or if your Perl script is not otherwise being directly executed by the shell. .PP Patches are welcome to make other platforms work more reliably. .SH "AUTHOR" .IX Header "AUTHOR" Author: Graham Ollis .PP Contributors: .PP Buddy Burden (\s-1BAREFOOT\s0) .PP Julien Fiegehenn (\s-1SIMBABQUE\s0) .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2012 by Graham Ollis. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.