bergman on 22 Jan 2009 12:18:14 -0800


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

Re: [PLUG] What is my current shell?



In the message dated: Thu, 22 Jan 2009 14:16:02 EST,
The pithy ruminations from TuskenTower on 
<[PLUG] What is my current shell?> were:
=> All,
=> We have an oddball problem.  Is there any portable way to determine
=> what shell you are using once you are inside a script?

It's harder than it seems, isn't it...

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

Um, that would just echo the name of the script.

=> 
=> As you can see here, $SHELL does not change when you switch shells.

Correct.

=> [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
=> 
=> 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.

"wrong" is subjective, but I see what you mean.


The following hacked shell script does what you want (lightly tested on Solaris 
8, Irix 6.5, Linux (CentOS 5.2), netbsd 4.0). I'm sure that there are much 
more elegant ways of doing the same thing.

----------------------------------------------------------------
#! /bin/sh

OS=`uname -s`
PSCMD="ps -fp"
CMDFIELD=8

if [ $OS = "NetBSD" ] ; then
	PSCMD="ps -jp"
	CMDFIELD=10
fi

ppid=$$
# do the "grep" to eliminate any headers from ps (yes, there are ps options
# for this...but this is supposed to be a quick & portable hack...the
# NetBSD test is bad enough)
info=`$PSCMD $ppid | grep $ppid`

# two possibilities...$info has the name of the parent shell OR $info has
# the name of the shell script...

# don't use "grep -q", since that's not portable on (older) Solaris
echo $info | grep "$0" 1> /dev/null 2>&1
if [ $? = 0 ] ; then
	# $info has the name of the script itself, as in
	#	user   6947  6727  0 14:46 pts/1    00:00:00 /bin/sh ./whichshell
	# in this case, use the 3rd field (the parent id of the shellscript)
	# to get the name of the shell under which the script was launched
	# for now we ignore further levels of recursion
	ppid=`echo $info | awk '{print $3}'`
	info=`$PSCMD $ppid | grep $ppid`
fi

# now $info has the shell name, as in:
#	user   6727  6215  0 14:46 pts/1    00:00:00 bash

shell=`echo $info | sed -e "s/  */ /g" | cut -d" " -f$CMDFIELD`

# clean up the value..remove any path info
# remove any leading "-" (used to signify a login shell)
shell=`echo $shell|sed -e "s,.*/,,"`
echo $shell | grep "^\-" 1> /dev/null 2>&1
if [ $? = 0 ] ; then
	shell=`echo $shell|sed -e "s/^-//"`
	login="login shell"
else
	login="not a login shell"
fi


# sanity check...

if [ -f /etc/shells ] ; then
	sed -e "s,.*/,," /etc/shells | grep "^$shell\$" 1> /dev/null 2>&1
	if [ $? != 0 ] ; then
		echo "The shell may be \"$shell\" (${login}), but that is not in /etc/shells"
		exit
	fi
fi
echo "The shell is \"$shell\" (${login})"
----------------------------------------------------------------

=> 
=> thanks,
=> Amul

Thanks for the fun exercise.

Mark

=> ___________________________________________________________________________
=> 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
=> 



___________________________________________________________________________
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