This determines the name/type of shell that the Perl program is running in (i.e. csh, ksh, bash, tsh, etc.).
I ended up writing this because I needed to write to different files based upon the type of shell that the user is running.
#!/usr/bin/perl -w use strict; my $ppid = getppid(); my $shell = `/usr/bin/ps -p $ppid`; $shell = (split(/\s+/, $shell))[$#_]; print qq(The parent pid is $ppid\n); print qq(The name of the parent shell is $shell\n);

Replies are listed 'Best First'.
Re: What shell am I running?
by rob_au (Abbot) on Nov 17, 2001 at 09:40 UTC
    As others have already pointed out, this code isn't overly portable because of the dependency on the output of ps. A better solution would be to make use of Proc::ProcessTable which I have reviewed previously here. eg.

    #!/usr/bin/perl -w use strict; use Proc::ProcessTable; my $proc = Proc::ProcessTable->new; foreach (@{$proc->table}) { if ($_->pid eq getppid) { print "The parent pid is ", getppid, "\n"; print "The name of the parent shell is ", $_->cmndline, "\n"; }; };

     

    Ooohhh, Rob no beer function well without!

Re: What shell am I running?
by blakem (Monsignor) on Nov 17, 2001 at 03:39 UTC
    You're using $#_ improperly here. If I add something like:
    @_ = ('a','b','c');
    before your split line, the split wont return the correct field anymore... Can you see why?

    -Blake

Re: What shell am I running?
by data64 (Chaplain) on Nov 17, 2001 at 06:20 UTC
    I wonder if someone can come up with a one liner for this.
    Yup, a golf challenge.
      It's probably not portable, but this works on linux:
      perl -le 'print$ENV{SHELL}'

      -Blake

        You can shave one char off of that; the -l doesn't do anything. Other then that, I think that's as short as it gets for shells that set ${SHELL} (which is POSIX, I think). (Note that the original isn't portable past whatever Unix they were using; ps's format isn't portable at least to my Linux. For that matter, mine doesn't need the -p parameter.)

        Thanks,
        James Mastros,
        Just Another Perl Scribe

Re: What shell am I running?
by data64 (Chaplain) on Nov 17, 2001 at 06:43 UTC
    Does not work in my cygwin bash shell.
    Is -p a standard parameter to ps ?