JP Vossen on 22 Jan 2009 13:52:24 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] What is my current shell?


> Date: Thu, 22 Jan 2009 14:16:02 -0500
> From: TuskenTower <tuskentower@gmail.com>
> 
> We have an oddball problem.  Is there any portable way to determine
> what shell you are using once you are inside a script?

Doing "portable" with shell scripts is really hard. :-(  The GNU utils 
are partly responsible for this, since they spoil you on Linux.  Your 
best bet is to develop on the oldest, crappiest, least common 
denominator system you can find, and go from there.  If you try to do it 
on Linux, you will paint yourself into a lot of corners.


> Right now, we are thinking of using
> which `echo $0`

That works at the CLI but not inside a script; I just get the name of 
the shell script.

You can take a look here, but nothing I tried really worked either:
http://www.bashcookbook.com/bashinfo/source/bash-3.2/doc/bashref.html#SEC26

Obviously that's bash, but much of bash behavior is shared by the Bourne 
family.


> As you can see here, $SHELL does not change when you switch shells.
> [2:07pm] [shaha:pts/8] [shaha] : /gtc/staff/shaha/work/sortT > echo $SHELL
> /usr/local/bin/tcsh
> [2:08pm] [shaha:pts/8] [shaha] : /gtc/staff/shaha/work/sortT > bash
> shaha@shaha:~/work/sortT$  echo $SHELL
> /usr/local/bin/tcsh

Huh, I'd have thought $SHELL would work.  Bummer.

This works on Linux (hardy and etch) and Solaris 9i:
	ps -f -p $$ | tail -1 | awk '{print $8}'

But the 'ps' command is one of the ones that really, really, SUCKS for 
portability and this may not work on other systems.  I talk about 
portability issues a little in recipe 1.15 and 15.1-6 of the _bash 
Cookbook_.  VMware (or whatever) is your friend, I have Solaris (Intel) 
8,9,10, {Free,Net,Open}BSD.  But you are toast for AIX and HP-UX since 
those don't (didn't?) run on x86.  polarhome.com might be useful for 
testing those.  www.testdrive.hp.com was shut down effective September 
30, 2008 and replaced by a new virtual program.  It is not clear to me 
if there is a charge to sign up for it.

I haven't tested that ps command on anything but what is listed above. 
And it seems really silly that there isn't a better way, but I can't 
think of one at the moment.  I'd say a perl one-liner but haven't 
thought of that yet either.

(Ah, I just saw more inclusive answer along the same lines from Mark. 
Nice.)


> This is for problems at customer sites where they are using the wrong
> Bourne shell on Solaris (/bin/sh) when we want them to use
> /usr/xpg4/bin/sh.

Depending on what you are doing and how much control you have of the 
scripts and environment, this may or may not help, but recipe 15.2 talks 
about using the getconf tool to set a POSIX path.  Here is what you get 
on my Solaris 9i VM:
	bash-2.05$ getconf PATH
	/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin


On Thu, Jan 22, 2009 at 4:05 PM, Eric <eric@lucii.org> wrote:
 > > Try this:
 > > ps a | grep "^ *$$" | grep -v grep | awk '{print $5}'

I don't see how to do it in this case, but just as a general note, 
instead of the ugly '| grep -v grep |' hack, you can use a regex in the 
search field.  For example, to find all SSH processes (Linux):
	ps auwx | grep '[s]sh'

That works because grep interprets '[s]sh' as "ssh" and that doesn't 
match '[s]sh' so you get what you meant without the '| grep -v grep |' 
hack.  I confess I don't see a way around that hack in this case though.

Later,
JP
----------------------------|:::======|-------------------------------
JP Vossen, CISSP            |:::======|      http://bashcookbook.com/
My Account, My Opinions     |=========|      http://www.jpsdomain.org/
----------------------------|=========|-------------------------------
"Microsoft Tax" = the additional hardware & yearly fees for the add-on
software required to protect Windows from its own poorly designed and
implemented self, while the overhead incidentally flattens Moore's Law.

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug