Section 8.11. Enhancements


[Page 323 (continued)]

8.11. Enhancements

In addition to the new facilities that have already been described, the C shell also enhances the common core shell facilities in the following areas:

  • a shortcut for command re-execution

  • the {} metacharacters

  • filename substitution

  • redirection

  • piping

  • job control


[Page 324]

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 Substitution

In 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 Substitution

To 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 Situations

If 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. % _ 



[Page 325]

8.11.3. Redirection

In 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 Channel

To 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 Overwrites

You 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. Piping

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

Figure 8-31. Example of a piping of both stdout and stderr.

process1 |& process2



[Page 326]

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 Control

The job control facilities of the C shell are the same as the Korn shell's, with the following additional built-in commands:

  • stop

  • suspend

  • nice

  • nohup

  • notify

These commands are described below.

8.11.5.1. stop

To suspend a specified job, use the stop command (Figure 8-32).

Figure 8-32. Description of the stop shell command.

Shell Command: stop { %job }*

stop suspends the jobs that are specified using the same standard job specifier format described in Chapter 7, "The Korn Shell." If no arguments are supplied, the last-referenced job is suspended.



[Page 327]
8.11.5.2. suspend

Figure 8-33 describes the suspend shell command.

Figure 8-33. Description of the suspend shell command.

Shell Command: suspend

suspend suspends the shell that invokes it. Doing this makes sense only when the shell is a subshell of the login shell, and it is most commonly done to suspend a shell invoked by the su or script utilities.


8.11.5.3. nice

To set the priority level of the shell or a command, use the nice command (Figure 8-34).

Figure 8-34. Description of the nice shell command.

Shell Command: nice [ +/- number ] [ command ]

nice runs command with priority level number. In general, the higher the priority, the slower the process will run. Only a super-user can specify a negative priority level. If the priority level is omitted, 4 is assumed. If no arguments are specified, the shell's priority level is set.


For more information about process priorities, consult Chapter 12, "Systems Programming."

8.11.5.4. nohup

To protect a command from hangup conditions, use the built-in nohup command (Figure 8-35).

Figure 8-35. Description of the nohup shell command.

Shell Command: nohup [ command ]

nohup executes command and protects it from hangup conditions. If no arguments are supplied, then all further commands executed from the shell are nohup'ed. Note that all background commands are automatically nohup'ed in the C shell.


8.11.5.5. notify

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


[Page 328]

Figure 8-36. Description of the notify shell command.

Shell Command: notify { %job }*

notify instructs the shell to inform you immediately when the specified jobs change state. Jobs must be specified following the standard job specifier format described in Chapter 7, "The Korn Shell." If no job is specified, the last-referenced job is used. To enable immediate notification of all jobs, set the $notify variable.


8.11.6. Terminating a Login Shell

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

  • Type a Control-D on a line by itself (as long as $ignoreeof is not set).

  • Use the built-in exit command.

  • Use the built-in logout command.

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.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net