Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!nisc.nyser.net!rodan!jdpeek From: jdpeek@rodan.acs.syr.edu (Jerry Peek) Newsgroups: comp.unix.questions Subject: Re: Login shell? Message-ID: <1506@rodan.acs.syr.edu> Date: 10 Dec 89 15:33:24 GMT References: <2975@usceast.UUCP> Reply-To: jdpeek@rodan.acs.syr.edu (Jerry Peek) Distribution: usa Organization: Syracuse University, Syracuse, NY Lines: 64 In article <2975@usceast.UUCP> sridhar@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@rodan.acs.syr.edu, JDPEEK@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