• Aucun résultat trouvé

History substitutions

Dans le document Operating System (Page 132-135)

History substitutions can be used to reintroduce sequences of words from previous commands, possibly altering them in the process. Thus, history sub-stitutions provide a general redo facility.

History substitutions begin with the character "!" and may begin anywhere in the input stream unless a history substitution is already in progress. A"!"

preceded by a backslash (\), or followed by a space, tab, newline, "= 11 or "( ", is treated as a literal" !" and its special meaning is suppressed. History sub-stitutions may also occur when an input line begins with "A ". This special abbreviation will be described later.

The text of any input line containing a history substitution is echoed on the terminal after the substitution has been carried out, so that the user can see the literal command that is being executed.

Commands entered at the terminal and consisting of one or more words are saved on the history list, the size of which is controlled by the history vari-able. The previous command is always retained. Commands are assigned numbers incrementally, starting with" 1 " (the first command executed under the current csh).

csh(C)

For example, enter the command:

history

This internal command causes csh to print a list of the commands stored on the history list, along with their event numbers. Now, consider the following (sample) output from the history command:

9 write michael 10 ex write.c 11 cat oldwrite.c 12 diff *write.c

It is not usually necessary to use event numbers, but the current event number can be made part of the prompt by placing a " ! " in the prompt string.

If the current event (the current command line) is 13, we can refer to previous command lines in several ways:

By event number:

!11

to re-run cat oldwrite. c By relative event number:

!-2

to go back two events; this will also re-run cat oldwri te. c By part of a command:

!d

will re-run the most recent command starting with a "d", in this case diff

*write. c, while:

!?mic?

will re-run the most recent command containing the string "mic"; write michael

These forms simply reproduce the words of the specified event, each separated by a single blank. The special case"!!" refers to the previous com-mand; thus the history substitution 1/ !!" means "repeat the last command."

The form" !#" references the current command (the one being entered on the current line). It allows a word to be selected from further left in the line, for example to avoid retyping a long name, as in " !#:1".

csh(C)

To select words from an event, we can follow the event specification by a colon (:) and a designator for the desired words. The words of an input line are numbered from 0, the first (usually command) word being 0, the second word (first argument) being I, and so on. The basic word designators are:

o

First (command) word n nth argument

First argument, that is, 1

$ Last argument

% Word matched by (immediately preceding) ?s? search x-y Range of words

-y Abbreviates O-y

*

Abbreviates ~ -$, or nothing if only 1 word in event x* Abbreviates x-$

x- Like x* but omitting word $

The" : /I separating the event specification from the word designator can be omitted if the argument selector begins with a ~,$, *, -or %. After the optional word designator, a sequence of modifiers can be placed, each preceded by a colon. The following modifiers are defined:

h Removes a trailing pathname component r Removes a trailing .xxx component

e Returns the trailing .xxx pathname component sllirl Substitutes r for I

t Removes all leading pathname components

& Repeats the previous substitution

g Applies the change globally, prefixing the above p Prints the new command but does not execute it q Quotes the substituted words, preventing substitutions x Like q, but breaks into words at blanks, tabs, and newlines

Unless preceded by a " g ", the modification is applied only to the first modifi-able word. In any case it is an error for no word to be applicmodifi-able.

csh(C)

The left sides of substitutions are not regular expressions like those recog-nized by the editors, but rather strings. Any character may be used as the de-limiter instead of " /"; if it is necessary to include an instance of the dede-limiter character within one of the substitution strings, its special meaning may be removed by preceding it with a 1/ \ " . An ampersand character (&) in the right side of a substitution is replaced by the text from the left side of the substitu-tion. An ampersand preceded by a backslash (\&) is treated as a literal ampersand (&) with no special meaning. A null 1 uses the previous string either from an 1 or from a contextual scan string 5 in "!?s?/I. The trailing de-limiter in the substitution may be omitted if a newline follows immediately, as may the trailing" ? /I in a contextual scan.

A history reference may be given without an event specification (for example,

!$). It is assumed that the reference is to the previous command unless a his-tory substitution precedes it on the same line, in which case it is assumed to refer to the last event substitution. Thus !?foon$ gives the first and last argu-ments from the command matching ?foo?

A special abbreviation of a history reference occurs when the first nonblank character of an input line is a "A /I. This is equivalent to !:SA, providing a con-venient shorthand for substitutions on the text of the previous line. Thus AlbAlib fixes the spelling of lib in the previous command. Finally, a history substitution may be surrounded with "{ /I and "} /I if necessary to insulate it from the characters that follow. Thus, after Is -ld Npaul we might do !{l}a to do Is -ld Npaula, while !la would look for a command starting lao

Dans le document Operating System (Page 132-135)