home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 2.6 Use Absolute Pathnames in Shell Setup Files Chapter 2
Logging In
Next: 2.8 Identifying Login Shells
 

2.7 C Shell Setup Files Aren't Read When You Want Them to Be?

The C shell reads its .cshrc , .login , and .logout setup files at particular times (2.2 ) . Only "login" C shells (2.8 ) will read the .login and .logout files. Back when csh was designed, this restriction worked fine. The shell that started as you logged in was flagged as a login shell, and it read all three files. You started other shells (shell escapes, shell scripts, etc.) from that login shell, and they would read only .cshrc .

Now, UNIX has interactive shells started by window systems (like xterm (1.31 ) ), remote shells (like rsh (1.33 ) ), and other shells that might need some things set from the .login or .logout files. Depending on how these shells are invoked, these might not be login shells - so they might read only .cshrc . How can you handle that? Putting all your setup commands in .cshrc isn't good because all subshells (38.4 ) read it... you definitely don't want to run terminal-setting commands like tset during shell escapes!

To handle problems at login time, put almost all of your setup commands in .cshrc instead of .login . After the "login-only" commands have been read from .cshrc , set the ENV_SET environment variable (6.1 ) as a flag. (There's nothing special about this name. You can pick any name you want.)

The shell will copy the "flag" variable to subshells and the .cshrc can test for it - if the variable exists, the login-only commands are skipped. That'll keep the commands from being read again in a shell escape.

Here are parts of a .cshrc that show the idea:


if
 $?
 





...Normal .cshrc stuff
...
if ($?prompt && ! $?ENV_SET) then
    # Do commands that used to go in .login file:
    setenv EDITOR /usr/ucb/vi
    tset
        ...
    setenv ENV_SET done
endif

You should put a comment in the .login file to explain what you've done.

The .logout file should probably be read only once - when your last ("top-level") shell exits. If your top-level shell isn't a login shell, you can make it read .logout anyway. Here's how. First, along with the previous fixes to your .cshrc file, add an alias that will read your .logout file when you use the exit command. Also set the ignoreeof variable (3.5 ) to force you to use the exit command when you log out. Now the chunk of your .cshrc will look like:




""exit
 


if ($?prompt && ! $?ENV_SET) then
        ...
    # Make all top-level interactive shells read .logout file:
    set ignoreeof

    alias exit 'source ~/.logout; ""exit'
        ...
endif

- JP


Previous: 2.6 Use Absolute Pathnames in Shell Setup Files UNIX Power Tools Next: 2.8 Identifying Login Shells
2.6 Use Absolute Pathnames in Shell Setup Files Book Index 2.8 Identifying Login Shells

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System