Interactive Shell or Top Level Shell

How do I tell inside .cshrc if I’m a login shell?
When people ask this, they usually mean either How can I tell if it’s an interactive shell? or How can I tell if it’s a top-level shell?

You could perhaps determine if your shell truly is a login shell (i.e. is going to
source “.login” after it is done with “.cshrc”) by fooling around with “ps” and
“$$”. Login shells generally have names that begin with a ‘-’. If you’re really
interested in the other two questions, here’s one way you can organize your
.cshrc to find out.

           if (! $?CSHLEVEL) then #
                     # This is a “top-level” shell, # perhaps a login shell, perhaps a shell started up by # ‘rsh machine some-command’
                     # This is where we should set PATH and anything else we
                     # want to apply to every one of our shells.
                     #
                       setenv CSHLEVEL 0
                       set home = ~username # just to be sure source
                                       ~/.env # environment stuff we always want else #
                    # This shell is a child of one of our other shells so 
                    # we don’t need to set all the environment variables again.
                    #
                       set tmp = $CSHLEVEL
                   @ tmp++
                       setenv CSHLEVEL $tmp endif # Exit from .cshrc if not
                        interactive, e.g. under rsh if (! $?prompt) exit # Here we could set the prompt or
                        aliases that would be useful # for interactive shells only.
                         source ~/.aliases How do I construct a shell glob-pattern that matches
                         all files except “.” and “..” ?

You’d think this would be easy.
* Matches all files that don’t begin with a “.”; .
* Matches all files that do begin with a “.”, but this includes the special entries “.” and “..”, which often you don’t want; .[!.]* (Newer shells only; some shells use a “^” instead of the “!”; POSIX shells must accept the “!”, but may accept a “^” as well; all portable applications shall not use an unquoted “^” immediately following the “[“) Matches all files that begin with a “.” and are followed by a non-”.”; unfortunately this will miss “..foo”; .??* Matches files that begin with a “.” and which are at least 3 characters long. This neatly avoids “.” and “..”, but also misses “.a” .

So to match all files except “.” and “..” safely you have to use 3 patterns (if you don’t have filenames like “.a” you can leave out the first): .[!.]* .??* *

Alternatively you could employ an external program or two and use backquote substitution. This is pretty good: `ls -a | sed -e ‘^\.$d’ -e ‘^\.\.$d’` (or `ls -A` in some Unix versions) but even it will mess up on files with newlines, IFS characters or wildcards in their names.

Relevance Read:

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.