1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
NAME
Shell::Guess - Make an educated guess about the shell in use
VERSION
version 0.10
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";
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.
* getpwent
On UNIXy systems with getpwent, that can be used to determine the
login shell.
* dscl
Under Mac OS X getpwent will typically not provide any useful
information, so the dscl command is used instead.
* 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.
* ps
On UNIXy systems without a proc filesystem, Shell::Guess will use the
ps command to determine the running shell.
* Win32::Getppid and 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, command.com and cmd.exe.
* ComSpec
If the above method is inconclusive, the ComSpec environment variable
will be consulted to differentiate between command.com or cmd.exe
(PowerShell cannot be detected in this manner).
* 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.
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 eval and
backticks or 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
Shell::Config::Generate provides a portable interface for generating
such shell configurations, and is designed to work with this module.
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.
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.
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.
bash_shell
my $shell = Shell::Guess->bash_shell;
Returns an instance of Shell::Guess for bash.
The following instance methods will return:
* $shell->name = bash
* $shell->is_bash = 1
* $shell->is_bourne = 1
* $shell->is_unix = 1
* $shell->default_location = /bin/bash
All other instance methods will return false
bourne_shell
my $shell = Shell::Guess->bourne_shell;
Returns an instance of Shell::Guess for the bourne shell.
The following instance methods will return:
* $shell->name = bourne
* $shell->is_bourne = 1
* $shell->is_unix = 1
* $shell->default_location = /bin/sh
All other instance methods will return false
c_shell
my $shell = Shell::Guess->c_shell;
Returns an instance of Shell::Guess for c shell.
The following instance methods will return:
* $shell->name = c
* $shell->is_c = 1
* $shell->is_unix = 1
* $shell->default_location = /bin/csh
All other instance methods will return false
cmd_shell
my $shell = Shell::Guess->cmd_shell;
Returns an instance of Shell::Guess for the Windows NT cmd shell
(cmd.exe).
The following instance methods will return:
* $shell->name = cmd
* $shell->is_cmd = 1
* $shell->is_win32 = 1
* $shell->default_location = C:\Windows\system32\cmd.exe
All other instance methods will return false
command_shell
my $shell = Shell::Guess->command_shell;
Returns an instance of Shell::Guess for the Windows 95 command shell
(command.com).
The following instance methods will return:
* $shell->name = command
* $shell->is_command = 1
* $shell->is_win32 = 1
* $shell->default_location = C:\Windows\system32\command.com
All other instance methods will return false
dcl_shell
my $shell = Shell::Guess->dcl_shell;
Returns an instance of Shell::Guess for the OpenVMS dcl shell.
The following instance methods will return:
* $shell->name = dcl
* $shell->is_dcl = 1
* $shell->is_vms = 1
All other instance methods will return false
fish_shell
my $shell = Shell::Guess->fish_shell;
Returns an instance of Shell::Guess for the fish shell.
The following instance methods will return:
* $shell->name = fish
* $shell->is_fish = 1
* $shell->is_unix = 1
korn_shell
my $shell = Shell::Guess->korn_shell;
Returns an instance of Shell::Guess for the korn shell.
The following instance methods will return:
* $shell->name = korn
* $shell->is_korn = 1
* $shell->is_bourne = 1
* $shell->is_unix = 1
* $shell->default_location = /bin/ksh
All other instance methods will return false
power_shell
my $shell = Shell::Guess->power_shell;
Returns an instance of Shell::Guess for Microsoft PowerShell (either
for Windows powershell.exe or Unix pwsh).
The following instance methods will return:
* $shell->name = power
* $shell->is_power = 1
* $shell->is_win32 = 1
All other instance methods will return false
tc_shell
my $shell = Shell::Guess->tc_shell;
Returns an instance of Shell::Guess for tcsh.
The following instance methods will return:
* $shell->name = tc
* $shell->is_tc = 1
* $shell->is_c = 1
* $shell->is_unix = 1
* $shell->default_location = /bin/tcsh
All other instance methods will return false
z_shell
my $shell = Shell::Guess->z_shell
Returns an instance of Shell::Guess for zsh.
The following instance methods will return:
* $shell->name = z
* $shell->is_z = 1
* $shell->is_bourne = 1
* $shell->is_unix = 1
* $shell->default_location = /bin/zsh
All other instance methods will return false
INSTANCE METHODS
The normal way to call these is by calling them on the result of either
running_shell or login_shell, but they can also be called as class
methods, in which case the currently running shell will be used, so
Shell::Guess->is_bourne
is the same as
Shell::Guess->running_shell->is_bourne
is_bash
my $bool = $shell->is_bash;
Returns true if the shell is bash.
is_bourne
my $bool = $shell->is_bourne;
Returns true if the shell is the bourne shell, or a shell which
supports bourne syntax (e.g. bash or korn).
is_c
my $bool = $shell->is_c;
Returns true if the shell is csh, or a shell which supports csh syntax
(e.g. tcsh).
is_cmd
my $bool = $shell->is_cmd;
Returns true if the shell is the Windows command.com shell.
is_command
my $bool = $shell->is_command;
Returns true if the shell is the Windows cmd.com shell.
is_dcl
my $bool = $shell->is_dcl;
Returns true if the shell is the OpenVMS dcl shell.
is_fish
my $bool = $shell->is_fish;
Returns true if the shell is Fish shell.
is_korn
my $bool = $shell->is_korn;
Returns true if the shell is the korn shell.
is_power
my $bool = $shell->is_power;
Returns true if the shell is Windows PowerShell.
is_tc
my $bool = $shell->is_tc;
Returns true if the shell is tcsh.
is_unix
my $bool = $shell->is_unix;
Returns true if the shell is traditionally a UNIX shell (e.g. bourne,
bash, korn)
is_vms
my $bool = $shell->is_vms;
Returns true if the shell is traditionally an OpenVMS shell (e.g. dcl)
is_win32
my $bool = $shell->is_win32;
Returns true if the shell is traditionally a Windows shell
(command.com, cmd.exe, powershell.exe, pwsh)
is_z
my $bool = $shell->is_z;
Returns true if the shell is zsh
name
my $name = $shell->name;
Returns the name of the shell.
default_location
my $location = $shell->default_location;
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.
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.
These are the operating systems that have been tested in development
and are most likely to guess reliably.
* Linux
* Cygwin
* FreeBSD
* Mac OS X
* Windows (Strawberry Perl)
* Solaris (x86)
* MS-DOS (djgpp)
* OpenVMS
Always detected as dcl (a more nuanced view of OpenVMS is probably
possible, patches welcome).
UNIXy platforms without a proc filesystem will use Unix::Process if
installed, which will execute ps to determine the running shell.
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.
Patches are welcome to make other platforms work more reliably.
AUTHOR
Author: Graham Ollis <plicease@cpan.org>
Contributors:
Buddy Burden (BAREFOOT)
Julien Fiegehenn (SIMBABQUE)
COPYRIGHT AND LICENSE
This software is copyright (c) 2012-2023 by Graham Ollis.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
|