How would I determine what script, program, or shell executed my Perl script?
Example: I might want to have human readable output if executed from shell (customized for each type of shell), a different type of output if called as a script from another perl script, and a machine readable format if executed from a program such as a continuous integration server.
Motivation: I have a tool that changes its output based on which shell executes it. I'd normally implement this behavior as an option to the script, but this tool's design doesn't allow for options. Other shells have environment variables that indicate what shell is running. I'm working on a patch to support Powershell, which has no such special variable.
Edit: Many of these answers happen to be linux specific. Unfortuantely, Powershell is for Windows. getppid
, the $ENV{SHELL}
variable, and shelling out to ps
won't help in this case. This script needs to run cross-platform.
-t
) and, if so, assume that you've been invoked from an interactive shell rather than, say, a continuous integration daemon. However this common technique doesn't speak to customizing output per-distinct-possible-parent (but I think that's a bit of a misplaced desire, anyway). - pilcrowgetppid
would do wonders. This script needs to work on Windows, specifically, with Powershell, andgetppid
isn't implemented in Strawberry Perl or ActiveState Perl. - Robert P