Previous Section  < Day Day Up >  Next Section

9.2. The Environment

9.2.1 Initialization Files

After 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 Files

The .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

  1. If the prompt has been set ($?prompt), the shell is running interactively; that is, it is not running in a script.

  2. The primary prompt is set to the number of the current history event, the name stardust, and a > character. This will change the % prompt, the default.

  3. The history variable is set to 32. This controls the number of history events that will appear on the screen. The last 32 commands you entered will be displayed when you type history (see "Command-Line History" on page 413).

  4. Normally, when you log out, the history list is cleared. The savehist variable allows you to save a specified number of commands from the end of the history list. In this example, the last 5 commands will be saved in a file in your home directory, the .history file, so that when you log in again, the shell can check to see if that file exists and put the history lines saved at the top of the new history list.

  5. The noclobber variable is set to protect the user from inadvertently removing files when using redirection. For example, sort myfile > myfile will destroy myfile. With noclobber set, the message file exists will appear on the screen.

  6. The filec variable is used for filename completion so that you only need to type the first number of significant characters in a filename, press the Esc key, and the shell will complete the rest of the filename. By pressing ^D (Ctrl-D) when typing in the filename, the C shell will display a list of files that match that string. The fignore variable allows you to exclude files that you do not want affected by filename completion. In this case, all the .o filenames (object files) will not be affected by filec, even though filec is set (see "Filename Completion: The filec Variable" on page 430).

  7. The cdpath variable is assigned a list of path elements. When changing directories, if you specify just the directory name, and that directory is not a subdirectory directly below the current working directory, the shell will search the cdpath directory entries to see if it can find the directory in any of those locations and then will change the directory.

  8. The ignoreeof variable prevents you from logging out with ^D. UNIX utilities that accept input from the keyboard, such as the mail program, are terminated by pressing ^D. Often, on a slow system, the user will be tempted to press ^D more than once. The first time, the mail program would be terminated, the second time, the user is logged out. By setting ignoreeof, you are required to type logout to log out.

  9. The aliases are set to give a shorthand notation for a single command or group of commands. Now when you type the alias, the command(s) assigned to it will be executed. The alias for the more command is m. Every time you type m, the more command is executed. The status alias prints the date and a summary of the user's disk usage. The cd alias creates a new prompt every time the user changes directory. The new prompt will contain the number of the current history event (\!*) and the current working directory, $cwd surrounded by < > (see "Aliases" on page 418).

The .login File

The .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

  1. The stty command sets options for the terminal. Input characters will not be stripped to seven bits if –istrip is used.

  2. The stty command sets Ctrl-H, the Backspace key, to erase.

  3. Any line beginning with # is a comment. It is not an executable statement.

  4. If the current terminal window (tty) is the console (linux), the next line is executed; otherwise, program control goes to the last endif.

  5. This line is echoed to the screen, and if the user does not press Ctrl-C to kill the process, the program will sleep (pause) for five seconds, and then the X Windows program will start.

  6. The startx program launches X Windows if this system is Linux.

  7. The endif marks the end of the innermost if construct.

  8. The autologout variable is set to 60 so that after 60 minutes of inactivity, the user will automatically be logged out (from the login shell).

9.2.2 The Search Path

The 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.

[1] Do not confuse the search path variable with the cdpath variable set in the .cshrc file.


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 Command

The 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 Command

The 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]

[2] On systems without vfork, prints the number and size of hash buckets; for example, 1024 hash buckets of 16 bits each.

9.2.5 The source Command

The 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 Prompts

The 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

  1. The primary prompt is assigned the user's login name, followed by a > symbol and a space.

  2. The new prompt is displayed.

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

  1. This is an example of online scripting. Because the C shell is expecting further input after the foreach loop is entered, the secondary prompt appears. The foreach loop processes each word in the parenthesized list.

  2. The first time in the loop, joe is assigned to the variable pal. The user joe is sent the contents of memo in the mail. Then next time through the loop, tom is assigned to the variable pal, and so on.

  3. The end statement marks the end of the loop. When all of the items in the parenthesized list have been processed, the loop ends and the primary prompt is displayed.

  4. The primary prompt is displayed.

    Previous Section  < Day Day Up >  Next Section