Operating System - HP-UX
1824297 Members
4710 Online
109669 Solutions
New Discussion юеВ

Determining user's current shell

 
SOLVED
Go to solution
Deepak Extross
Honored Contributor

Determining user's current shell

I need to check if the user's current shell is csh.
$shell isn't foolproof, and I'm sure there must be a more elegant alternative to running a csh-specific command and checking $?.
Any ideas?
11 REPLIES 11
Steven Sim Kok Leong
Honored Contributor

Re: Determining user's current shell

Hi,

login shell can be extracted from /etc/passwd. For current shell, I prefer the easier way of checking the last access timestamp of the .profile, .cshrc, .bashrc, .dtprofile etc.

1) Check user profile access time, eg. for csh,

$ ls -lu .cshrc
- -rw-r--r-- 1 user1 users 814 Feb 18 20:02 .cshrc
$ csh
sys 21: ls -lu .cshrc
- -rw-r--r-- 1 user1 users 814 Feb 19 13:50 .cshrc

2) Use syntax testing:

For sh/ksh/bash,
# export a=1

For csh, tcsh,
# echo >/dev/null 2>&1
Ambiguous output redirect.
# echo $?
Variable syntax.

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: Determining user's current shell

Hi,

One issue with using the last access timestamps on .cshrc user profile is that the user can skip its execution (if he wants to) by executing:

# csh -f

Thus, a syntax check though more inconvenient is still more reliable:

For sh/ksh/bash:
# setenv
sh: setenv: not found.

For csh:
sys 21: setenv
setenv: Too few arguments.

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: Determining user's current shell

Hi,

The $SHELL variable only works for the login shell which can be extracted from /etc/passwd:

$ echo $SHELL
/usr/bin/sh
$ csh
sys 21: echo $SHELL
/usr/bin/sh

If you wish to prevent $SHELL from being tampered, you can add this in your /etc/profile between the traps:

readonly SHELL

Hope this helps. Regards.

Steven Sim Kok Leong
SHABU KHAN
Trusted Contributor

Re: Determining user's current shell

hi,

how about:

env | egrep 'csh|ksh'

-Shabu
Deepak Extross
Honored Contributor

Re: Determining user's current shell

Hi Shabu,
I'm looking for current shell, not default shell.
To make matters worse, the user can have multiple telnet sessions open, each running a different shell.
Syntax-testing as suggested by Steven will definitely work, but I was looking for a more elegant solution.
Steven Sim Kok Leong
Honored Contributor

Re: Determining user's current shell

Hi Deepak,

For different windows, if the prompt is different for each profile, .cshrc etc then the prompt can be used to identify the shell a user is current running, ie.

sh/ksh/bash:

$ ksh
$
$

csh/tcsh:

$ csh
sys 21:
sys 21:

In .cshrc,

set prompt = "$system \!: "

Hope this helps. Regards.
Steven Sim Kok Leong
Honored Contributor

Re: Determining user's current shell

Hi,

I think I have found the elegant solution you wanted:

Execute this:

# ps -fp `echo $$`

$ ksh
$ ps -fp `echo $$`
UID PID PPID C STIME TTY TIME COMMAND
user1 16088 16079 0 15:01:49 pts/0 0:00 ksh

$ csh
sys 21: ps -fp `echo $$`
UID PID PPID C STIME TTY TIME COMMAND
user1 16334 16088 0 15:06:28 pts/0 0:00 csh

sys 22: sh
$ ps -fp `echo $$`
UID PID PPID C STIME TTY TIME COMMAND
user1 16345 16334 0 15:06:40 pts/0 0:00 sh

You can thus incorporate this in your script eg:

shell_used=`ps -fp `echo $$`|tail -1|awk '{print $8}'`

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor
Solution

Re: Determining user's current shell

Hi,

The echo is redundant.

Instead of ps -fp `echo $$`, ps -fp $$ alone is sufficient.

# ps -fp $$|tail -1|awk '{print $8}'

shell_used=`ps -fp $$|tail -1|awk '{print $8}'`

Hope this helps. Regards.

Steven Sim Kok Leong
Deepak Extross
Honored Contributor

Re: Determining user's current shell

Thanks, Steven!
That deserves a bunny :-)
Robin Wakefield
Honored Contributor

Re: Determining user's current shell

Hi Deepak,

Probably best to just use "ps -p $$", then you don't have to worry about the timestamp being 1 or 2 fields long:

shell_used=`ps -p $$ | awk /:/'{print $4}'`

Rgds, Robin.
Steve Steel
Honored Contributor

Re: Determining user's current shell

Hi

If you start from another shell or not.

shell in lowercase is set to /usr/bin/csh if you are using csh.

Otherwise if your shell has been open for a while there can be 9 parameters with the 9th as shell name

This works



#!/sbin/sh
MYSHELL=$(ps -fp $$|tail -1|sed -e 's/^.* //' -e 's/-//')

run as . showshell

Then MYSHELL is correct or adjust the previous
entry.

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)