9.8. Filename Substitution

 <  Day Day Up  >  

When evaluating the command line, the shell uses metacharacters to abbreviate filenames or pathnames that match a certain set of characters . The filename substitution metacharacters listed in Table 9.4 are expanded into an alphabetically listed set of filenames. The process of expanding a metacharacter into filenames is also called globbing . Unlike the other shells , when the C shell cannot substitute a filename for the metacharacter it is supposed to represent, the shell reports No match .

Table 9.4. Shell Metacharacters and Filename Substitution

Metacharacter

Meaning

*

Matches zero or more characters

?

Matches exactly one character

[abc]

Matches one character in the set: a , b , or c

[a “z]

Matches one character in the range a to z

{a, ile,ax}

Matches for a character or set of characters

~

Substitutes the user 's home directory for tilde

\

Escapes or disables the metacharacter


The shell performs filename substitution by evaluating its metacharacters and replacing them with the appropriate letters or digits in a filename.

9.8.1 The Asterisk

The asterisk matches zero or more characters in a filename.

Example 9.28.
 1   %  ls   a.c b.c abc ab3 file1 file2 file3 file4 file5  2   %  echo *   a.c b.c abc ab3 file1 file2 file3 file4 file5  3   %  ls  *.c   a.c b.c  4   %  rm  z*p   No match.  

EXPLANATION

  1. All the files in the current directory are listed.

  2. The echo program prints all its arguments to the screen. The asterisk (also called a splat ) is a wildcard that means: match for zero or more of any characters found in a filename. All the files in the directory are matched and echoed to the screen.

  3. Filenames ending in .c are listed.

  4. Because none of the files in the directory start with z , the shell reports No match .

9.8.2 The Question Mark

The question mark matches exactly one character in a filename.

Example 9.29.
 1   %  ls   a.c b.c abc ab3 file1 file2 file3 file4 file5  2   %  ls ???   abc ab3  3   %  echo How are you?   No match.  4   %  echo How are you\?   How are you?  

EXPLANATION

  1. All the files in the current directory are listed.

  2. The question mark matches for a single-character filename. Any filenames consisting of three characters are listed.

  3. The shell looks for a filename spelled y “o “u followed by one character. There is not a file in the directory that matches these characters. The shell prints No match .

  4. The backslash preceding the question mark is used to turn off the special meaning of the question mark. Now the shell treats the question mark as a literal character.

9.8.3 The Square Brackets

The square brackets match a filename for one character from a set or range of characters.

Example 9.30.
 1   %  ls   a.c b.c abc ab3 file1 file2 file3 file4 file5 file10 file11 file12  2   %  ls file[123]   file1 file2 file3  3   %  ls [A-Za-z][a-z][1-5]   ab3  4   %  ls file1[0-2]   file10 file11 file12  

EXPLANATION

  1. All the files in the current directory are listed.

  2. Filenames starting with file and followed by a 1 , 2 , or 3 are matched and listed.

  3. Filenames starting with a letter (either uppercase or lowercase) followed by a lowercase letter, and followed by a number between 1 and 5 are matched and listed.

  4. Filenames starting with file1 and followed by a , 1 , or 2 are listed.

9.8.4 The Curly Braces

The curly braces ( {} ) match for a character or string of characters in a filename.

Example 9.31.
 1   %  ls   a.c b.c abc ab3 ab4 ab5 file1 file2 file3 file4 file5 foo faa fumble  2   %  ls f{oo,aa,umble}   foo faa fumble  3   %  ls a{.c,c,b[3-5]}   a.c ab3 ab4 ab5  

EXPLANATION

  1. All the files in the current directory are listed.

  2. Files starting with f and followed by the strings oo , aa , or umble are listed. Spaces inside the curly braces will cause the error message Missing } .

  3. Files starting with a followed by .c , c , or b3 , b4 , or b5 are listed. (The square brackets can be used inside the curly braces.)

9.8.5 Escaping Metacharacters

The backslash is used to escape the special meaning of a single character. The escaped character will represent itself.

Example 9.32.
 1   %  gotta light?   No match.  2   %  gotta light\?   gotta: Command not found.  

EXPLANATION

  1. This is a little UNIX joke. The question mark is a file substitution metacharacter and evaluates to a single character. The shell looks for a file in the present working directory that contains the characters l “i “g “h “t , followed by a single character. If the shell cannot find the file, it reports No match . This shows you something about the order in which the shell parses the command line. The metacharacters are evaluated before the shell tries to locate the gotta command.

  2. The backslash protects the metacharacter from interpretation, often called escaping the metacharacter. Now the shell does not complain about a No match , but searches the path for the gotta command, which is not found.

9.8.6 Tilde Expansion

The tilde character by itself expands to the full pathname of the user's home directory. When the tilde is prepended to a username, it expands to the full pathname of that user's home directory. When prepended to a path, it expands to the home directory and the rest of the pathname.

Example 9.33.
 1   %  echo ~   /home/jody/ellie  2   %  cd ~/desktop/perlstuff  %  pwd   /home/jody/ellie/desktop/perlstuff  3   %  cd ~joe  %  pwd   /home/bambi/joe  

EXPLANATION

  1. The tilde expands to the user's home directory.

  2. The tilde followed by a pathname expands to the user's home directory, followed by /desktop/perlstuff .

  3. The tilde followed by a username expands to the home directory of the user. In this example, the directory is changed to that user's home directory.

9.8.7 Filename Completion: The filec Variable

When running interactively, the C/TC shell provides a shortcut method for typing a filename or username. The built-in filec variable, when set, is used for what is called filename completion. If you type the first few significant characters of a file in the current working directory and press the Esc key, the shell fills in the rest of the filename, provided that there are not a number of other files beginning with the same characters. If you type Ctrl-D after the partial spelling of the file, the shell will print out a list of files that match those characters. The terminal beeps if there are multiple matches. If the list begins with a tilde, the shell attempts to expand that list to a username.

Example 9.34.
 1   %  set filec  2   % ls  rum rumple rumplestilsken run2  3   %  ls ru[ESC]   [a]   # terminal beeps  4   %  ls rum^D   rum rumple rumplestilsken  5   %  ls rump[ESC]   rumple  6   %  echo ~ell[ESC]   /home/jody/ellie  

EXPLANATION

  1. The special C shell variable filec is set. Filename completion can be used.

  2. The files in the present working directory are listed.

  3. Filename completion is attempted. The letters r and u are not unique; that is, the shell does not know which one to pick, so it causes the terminal to beep.

  4. After the letters r “u “m are typed, ^D is pressed. A list of all filenames beginning with rum are displayed.

  5. The first filename starting with rump is completed and displayed.

  6. If a tilde precedes a partially spelled username, the shell will attempt to complete the spelling of the user's name and display the user's home directory.

9.8.8 Turning Off Metacharacters with noglob

If the noglob variable is set, filename substitution is turned off, meaning that all metacharacters represent themselves ; they are not used as wildcards. This can be useful when searching for patterns in programs like grep , sed , or awk , which may contain metacharacters that the shell may try to expand.

Example 9.35.
 1   %  set noglob  2   %  echo * ?? [] ~   * ?? [] ~  

EXPLANATION

  1. The variable noglob is set. It turns off the special meaning of the wildcards.

  2. The metacharacters are displayed as themselves without any interpretation.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

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