Login shell?

Jerry Peek jdpeek at rodan.acs.syr.edu
Mon Dec 11 02:33:24 AEST 1989


In article <2975 at usceast.UUCP> sridhar at usceast.uucp.UUCP (M. A. Sridhar) writes:
> Is there any way to determine, within a shell script (csh), whether the script
> is being executed by the login shell? I know that one can use the fact that
> $prompt is set to detect an interactive shell, but I don't know how to test
> whether it's the login shell.

This is a good excuse to post a nice set of code that I got from someone
(and hacked up myself).  It fixes your prompt so that it looks like this
in a top-level (login) shell:
	hostname%
On the first-level subshell, it looks like:
	(1) hostname%
and so on.

You could adapt this code for your needs.  You may want to think about
whether you want the following shells to be considered "login shells":
	- a shell started by the "su username" command
	- a shell started in a window by a windowing system like SunView
Also, if run your shell script with "csh -f", it won't read .cshrc --
so, the CSHLEVEL envariable won't be incremented -- that might or might
not be what you want.

This stuff could be cleaned up some.  I set it up to handle those
two special cases I mentioned above.  I'm not sure there's any one
"perfect" set of code for all situations.

Let's *NOT* start another round of "prompt wars", please!!

--Jerry Peek; Syracuse University Academic Computing Services; Syracuse, NY
  jdpeek at rodan.acs.syr.edu, JDPEEK at SUNRISE.BITNET        +1 315 443-3995

-------------- cut here; code below goes in .cshrc ---------------------
setenv HOST "`hostname | sed 's/\..*//'`"	# STRIP OFF DOMAIN STUFF

# SET SHELL LEVEL (DEPTH OF NESTED SHELLS).
# IF WE'VE ALREADY SET $CSHLEVEL ENVARIABLE BELOW AND THE $cshlevel SHELL
# VARIABLE IS NOT SET (THAT IS, WE'VE JUST STARTED A SUB-SHELL), INCREMENT
# $CSHLEVEL AND SET $cshlevel.
if ($?CSHLEVEL && (! $?cshlevel) ) then
	set cshlevel = $CSHLEVEL
	@ cshlevel++	# WE CAN'T DO "@ CSHLEVEL++", SO DO $cshlevel FIRST
	setenv CSHLEVEL $cshlevel
endif

# FOR PROMPTS WHEN WE EXECUTE res: SHOW DEPTH OF SHELL LAYER.
# $cshlevel IS SHELL VARIABLE, $CSHLEVEL IS COPY OF IT IN ENVIRONMENT.
# IF $CSHLEVEL UNSET, THEN THIS IS A REAL LOGIN (WE'RE NOT DOING source .cshrc):
if (! $?CSHLEVEL) then
	set cshlevel = 0
	setenv CSHLEVEL 0
endif

alias s_p 'set prompt = "${HOST}% "'
s_p

# IF THIS ISN'T A LEVEL-0 (TOP-LEVEL) SHELL, ADD LEVEL TO START OF $prompt.
# [DO IN TWO LINES BECAUSE USING if ($?cshlevel && $cshlevel != 0) GAVE
# THE ERROR cshlevel: undefined variable.  HOW GET SHORT-CIRCUIT EVALUATION??]
if ( $?cshlevel ) then
	if ($cshlevel != 0) then
		alias s_p 'set prompt="($cshlevel) $prompt"'
		s_p
	endif
endif



More information about the Comp.unix.questions mailing list