< Day Day Up > |
9.2. The Environment9.2.1 Initialization FilesAfter the csh/tcsh program starts, it is programmed to execute two files in the user's home directory: the .cshrc or .tcshrc file, respectively, and the .login file. These files allow users to initialize their own environments. The .cshrc and .tcshrc FilesThe .cshrc file contains C shell variable settings and is executed when you log in and every time a csh subshell is started. Likewise, the .tcshrc file contains TC shell variable settings and is executed when you log in and every time a tcsh subshell is started. These files share similar settings. For example, aliases and history are normally set here. Example 9.1.(The .cshrc File) 1 if ( $?prompt ) then 2 set prompt = "\! stardust > " 3 set history = 32 4 set savehist = 5 5 set noclobber 6 set filec fignore = ( .o ) 7 set cdpath = ( /home/jody/ellie/bin /usr/local/bin /usr/bin ) 8 set ignoreeof 9 alias m more alias status 'date;du -s' alias cd 'cd \!*;set prompt = "\! <$cwd> "' endif EXPLANATION
The .login FileThe .login file is executed one time when you first log in. It normally contains environment variables and terminal settings. It is the file where window applications are usually started. Because environment variables are inherited by processes spawned from this shell and only need to be set once, and terminal settings do not have to be reset for every process, those settings belong in the .login file. Example 9.2.(The .login File) 1 stty -istrip 2 stty erase ^h 3 stty kill ^u # # If possible start the windows system. # Give a user a chance to bail out # 4 if ( $TERM == "linux" ) then 5 echo "Starting X windows. Press Ctrl-C \ to exit within the next 5 seconds " sleep 5 6 startx 7 endif 8 set autologout=60 EXPLANATION
9.2.2 The Search PathThe path variable is used by the shell to locate commands typed at the command line. The search is from left to right. The dot represents the current working directory. If the command is not found in any of the directories listed in the path, or in the present working directory, the shell sends the message Command not found to standard error. It is recommended that the path be set in the .login file.[1] The search path is set differently in the C/TC shell than it is in the Bourne and Korn shells. Each of the elements is separated by whitespace.
set path = (/usr/bin /usr/ucb /bin /usr .) echo $path /usr/bin /usr/ucb /bin /usr . The environment variable PATH will display as echo $PATH /usr/bin:/usr/ucb:/bin:. The C/TC shell internally updates the environment variable for PATH to maintain compatibility with other programs (such as the Bourne or Korn shells) that may be started from this shell and will need to use the path variable. 9.2.3 The rehash CommandThe shell builds an internal hash table consisting of the contents of the directories listed in the search path. (If the dot is in the search path, the files in the dot directory, the current working directory, are not put in the hash table.) For efficiency, the shell uses the hash table to find commands that are typed at the command line, rather than searching the path each time. If a new command is added to one of the directories already listed in the search path, the internal hash table must be recomputed. This is done by typing
% rehash
(The % is the C shell prompt.) The hash table is also automatically recomputed when you change your path at the prompt or start another shell. 9.2.4 The hashstat CommandThe hashstat command displays performance statistics to show the effectiveness of its search for commands from the hash table. The statistics are in terms of "hits" and "misses." If the shell finds most of its commands you use at the end of your path, it has to work harder than if they were at the front of the path, resulting in a higher number of misses than hits. In such cases, you can put the most heavily hit directory toward the front of the path to improve performance. % hashstat 2 hits, 13 misses, 13%[2]
9.2.5 The source CommandThe source command is a shell built-in command, that is, part of the shell's internal code. It is used to execute a command or set of commands from a file. Normally, when a command is executed, the shell forks a child process to execute the command, so that any changes made will not affect the original shell, called the parent shell. The source command causes the program to be executed in the current shell, so that any variables set within the file will become part of the environment of the current shell. The source command is normally used to re-execute the .cshrc or .login if either has been modified. For example, if the path is changed after logging in, type % source .login or .cshrc 9.2.6 The Shell PromptsThe C shell has two prompts: the primary prompt, a percent sign (%), and the secondary prompt, a question mark (?). The TC shell uses a > as its primary default prompt. (See "The Shell Prompts" on page 461 for coverage of the tcsh enhanced prompt settings.) The primary prompt is the one displayed on the terminal after you have logged in; it waits for you to type commands. The primary prompt can be reset. If you are writing scripts at the prompt that require C/TC shell programming constructs, for example, decision making or looping, the secondary prompt will appear so that you can continue onto the next line. It will continue to appear after each newline, until the construct has been properly terminated. The secondary prompt cannot be reset. The Primary Prompt.When running interactively, the prompt waits for you to type a command and press the Enter key. If you do not want to use the default prompt, reset it in the .cshrc file and it will be set for this and all other C subshells. If you only want it set for this login session, set it at the shell prompt. Example 9.3.1 % set prompt = "$LOGNAME > " 2 ellie > EXPLANATION
The Secondary Prompt.The secondary prompt appears when you are writing online scripts at the prompt. Whenever shell programming constructs are entered, followed by a newline, the secondary prompt appears and continues to appear until the construct is properly terminated. Writing scripts correctly at the prompt takes practice. Once the command is entered and you press Enter, you cannot back up, and the C shell history mechanism does not save commands typed at the secondary prompt. Example 9.4.1 % foreach pal (joe tom ann) 2 ? mail $pal < memo 3 ? end 4 % EXPLANATION
|
< Day Day Up > |