8.11. EnhancementsIn addition to the new facilities that have already been described, the C shell also enhances the common core shell facilities in the following areas:
8.11.1. Metacharacters: {}You may use braces around filenames to save typing common prefixes and suffixes. The notation: a{b,c}d is textually replaced with: abd acd In the following example, I copied the C header files "/usr/include/stdio.h" and "/usr/include/signal.h" (which have a common prefix and suffix) into my home directory: % cp /usr/include/{stdio,signal}.h . ...copy two files. % _ 8.11.2. Filename SubstitutionIn addition to the common filename substitution facilities, the C shell supports two new featuresthe ability to disable filename substitution and the ability to specify what action should be taken if a pattern has no matches. 8.11.2.1. Disabling Filename SubstitutionTo disable filename substitution, set the $noglob variable. If this is done, wildcards lose their special meaning. The $noglob variable is not set by default. Here's an example: % echo a* p* ...one wildcard pair matches: p* prog1.c prog2.c prog3.c prog4.c % set noglob ...inhibit wildcard processing. % echo a* p* a* p* % _ 8.11.2.2. No-Match SituationsIf several patterns are present in a command and at least one of them has a match, then no error occurs. However, if none of the patterns has a match, the shell issues an error message by default. If the $nonomatch variable is set and no matches occur, then the original patterns are used as is. The $nonomatch variable is not set by default. Here's an example: % echo a* p* ...one wildcard pair matches: p*. prog1.c prog2.c prog3.c prog4.c % echo a* b* ...no wildcards match. echo: No match. ...error occurs by default. % set nonomatch ...set special nonomatch variable. % echo a* b* ...wildcards lose their special meaning. a* b* ...no error occurs. % _ 8.11.3. RedirectionIn addition to the common redirection facilities, the C shell supports a couple of enhancementsthe ability to redirect the standard error channel and the ability to protect files against accidental overwrites. 8.11.3.1. Redirecting the Standard Error ChannelTo redirect the standard error channel in addition to the standard output channel, simply append an & character to the > or >> redirection operator: % ls -l a.txt b.txt >list.out ...ls sends errors to stderr ls: File or directory "b.txt" is not found. % ls -l a.txt b.txt >& list.out ...also redirect stderr % _ Although there's no easy way to redirect just the error channel, it can be done using the following "trick": (process1 > file1) >& file2 This trick works by redirecting the standard output from process1 to file1 (which can be "/dev/null" if you don't want to save the output), allowing only the standard errors to leave the command group. The command group's output and error channels are then redirected to file2. 8.11.3.2. Protecting Files Against Accidental OverwritesYou may protect existing files from accidental overwrites, and nonexistent files from accidental appends, by setting the $noclobber variable. If a shell command tries to perform either action, it fails and issues an error message. Note that regular system calls such as write () are unaffected. $noclobber is not set by default. Here's an example: % ls -lG errors ...look at existing file. -rw-r-xr-x 1 glass 225 Feb 14 10:59 errors % set noclobber ...protect files. % ls a.txt >& errors ...cannot overwrite. errors: File exists. % _ To temporarily override the effect of $noclobber, append a ! character to the redirection operator: % ls a.txt >&! errors ...existing file is overwritten. % _ 8.11.4. PipingIn addition to the common piping facilities, the C shell also allows you to pipe the standard output and standard error channel from process1 to process2 using the syntax in Figure 8-31.
In the following example I piped the standard output and error channels from the ls utility to more: % ls -lG a.txt b.txt |& more ...pipe stdout and stderr. ls: File or directory "b.txt" is not found. -rw-r-xr-x 1 ables 988 Dec 7 06:27 a.txt % _ Although there's no direct way to pipe just the error channel, it can be done using a "trick" similar to the one used previously to pipe only the error channel to a file: (process1 > file) |& process2 This trick works by redirecting the standard output from process1 to file (which can be "/dev/null" if you don't want to save the output), allowing only the standard errors to leave the command group. The command group's output and error channels are then piped to process2, but because the standard output is now empty, the result is only the standard error output. 8.11.5. Job ControlThe job control facilities of the C shell are the same as the Korn shell's, with the following additional built-in commands:
These commands are described below. 8.11.5.1. stopTo suspend a specified job, use the stop command (Figure 8-32).
8.11.5.2. suspendFigure 8-33 describes the suspend shell command.
8.11.5.3. niceTo set the priority level of the shell or a command, use the nice command (Figure 8-34).
For more information about process priorities, consult Chapter 12, "Systems Programming." 8.11.5.4. nohupTo protect a command from hangup conditions, use the built-in nohup command (Figure 8-35).
8.11.5.5. notifyThe shell normally notifies you of a change in a job's state just before it displays a new prompt. If you want immediate (asynchronous) notification of job state changes, use the built-in notify command (Figure 8-36).
8.11.6. Terminating a Login ShellThe logout command terminates a login shell. Unlike exit, it cannot be used to terminate an interactive subshell. You may therefore terminate a login C shell in one of three ways:
Here's an example: % set ignoreeof ...set to prevent ^D exit. % ^D ...won't work now. Use "logout" to logout. % logout ...a better way to log out. login: _ When a login C shell is terminated, it searches for "finish-up" files. The commands in each file, if found, are executed in sequence. The user's finish-up file is $HOME/.logout and it is executed if found. Then, if the global finish-up file "/etc/csh.logout" exists, it is executed. A finish-up file typically contains commands for cleaning up temporary directories, other such clean-up operations, and perhaps a goodbye message. If a non-login C shell is terminated using exit or Control-D, no finish-up files are executed. |