11.8. Filename Substitution (Wildcards)

 <  Day Day Up  >  

When evaluating the command line, the shell uses metacharacters to abbreviate filenames or pathnames that match a certain set of characters , often called wildcards . The filename substitution metacharacters listed in Table 11.7 are expanded into an alphabetically listed set of filenames. The process of expanding a metacharacter into filenames is also called filename substitution, or globbing . If a metacharacter is used and there is no filename that matches it, the Korn shell treats the metacharacter as a literal character.

Table 11.7. 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

[!abc]

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

[a “z]

Matches one character in the range: any character in the set between a and z

~

Substitutes the user 's home directory for ~

\

Escapes or disables the metacharacter


11.8.1 The Asterisk

The asterisk is a wildcard that matches for zero or more of any character in a filename.

Example 11.27.
 1   $  ls *   abc abc1 abc122 abc123 abc2 file1 file1.bak file2 file2.bak none   nonsense noone nothing nowhere one  2   $  ls *.bak   file1.bak file2.bak  3   $  print a*c   abc  

EXPLANATION

  1. The asterisk expands to all of the files in the present working directory. All of the files are passed to ls and displayed.

  2. All files starting with zero or more characters and ending with .bak are matched and listed.

  3. All files starting with a , followed by zero or more characters, and ending in c are matched and passed as arguments to the print command.

11.8.2 The Question Mark

The question mark represents a single character in a filename. When a filename contains one or more question marks, the shell performs filename substitution by replacing the question mark with the character it matches in the filename.

Example 11.28.
 1   $  ls   abc abc1 abc122 abc123 abc2 file1 file1.bak file2 file2.bak   none nonsense noone nothing nowhere one  2   $  ls a?c?   abc1 abc2  3   $  ls ??   ?? not found  4   $  print abc???   abc122 abc123  5   $  print ??   ??  

EXPLANATION

  1. The files in the current directory are listed.

  2. Filenames containing four characters are matched and listed if the filename starts with an a , followed by a single character, followed by a c and a single character.

  3. Filenames containing exactly two characters are listed. There are none, so the two question marks are treated as literal characters. Because there is no file in the directory called ?? , the shell sends the message ?? not found .

  4. Filenames containing six characters are matched and printed, starting with abc and followed by exactly three of any character.

  5. The ksh print function gets the two question marks as an argument. The shell tries to match for any filenames with exactly two characters. There are no files in the directory that contain exactly two characters. The shell treats the question mark as a literal question mark if it cannot find a match. The two literal question marks are passed as arguments to the print command.

11.8.3 The Square Brackets

Brackets are used to match filenames containing one character from a set or range of characters.

Example 11.29.
 1   $  ls   abc abc1 abc122 abc123 abc2 file1 file1.bak file2 file2.bak   none nonsense noone nothing nowhere one  2   $  ls abc[123]   abc1 abc2  3   $  ls abc[13]   abc1 abc2  4   $  ls [az][az][az]   abc one  5   $  ls [!fz]???   abc1 abc2  6   $  ls abc12[2-3]   abc122 abc123  

EXPLANATION

  1. All of the files in the present working directory are listed.

  2. All four-character names are matched and listed if the filename starts with abc , followed by 1 , 2 , or 3 . Only one character from the set in the brackets is matched for a filename.

  3. All four-character filenames are matched and listed if the filename starts with abc , and is followed by a number in the range from 1 to 3 .

  4. All three-character filenames are matched and listed, if the filename contains exactly three lowercase alphabetic characters.

  5. All four-character files are listed if the first character is not a letter between f and z , followed by three of any character ( ??? ).

  6. Files are listed if the filenames contain abc12 , followed by 2 or 3 .

11.8.4 Escaping Metacharacters

To use a metacharacter as a literal character, use the backslash to prevent the metacharacter from being interpreted.

Example 11.30.
 1   $  ls   abc file1 youx  2   $  print How are you?   How are youx  3   $  print How are you\?   How are you?  4   $  print When does this line \   > ever end\?   When does this line ever end?  

EXPLANATION

  1. The files in the present working directory are listed. Note the file youx .

  2. The shell will perform filename expansion on the question mark. Any files in the current directory starting with y-o-u and followed by exactly one character are matched and substituted in the string. The filename youx will be substituted in the string to read, How are youx (probably not what you wanted to happen).

  3. By preceding the question mark ( ? ) with a backslash, it is escaped, meaning that the shell will not try to interpret it as a wildcard.

  4. The newline is escaped by preceding it with a backslash. The secondary prompt is displayed until the string is terminated with a newline. The question mark ( ? ) is escaped to protect it from filename expansion.

11.8.5 Tilde and Hyphen Expansion

The tilde character was adopted by the Korn shell (from the C shell) for pathname expansion. The tilde by itself evaluates to the full pathname of the user's home directory. When the tilde is appended with a username, it expands to the full pathname of that user.

The hyphen character refers to the previous working directory; OLDPWD also refers to the previous working directory.

Example 11.31.
 1   $  echo ~   /home/jody/ellie  2   $  echo ~joe   /home/joe  3   $  echo ~+   /home/jody/ellie/perl  4   $  echo ~   /home/jody/ellie/prac  5   $  echo $OLDPWD   /home/jody/ellie/prac  6   $  cd   /home/jody/ellie/prac  

EXPLANATION

  1. The tilde evaluates to the full pathname of the user's home directory.

  2. The tilde preceding the username evaluates to the full pathname of joe 's home directory.

  3. The ~+ notation evaluates to the full pathname of the working directory.

  4. The ~ “ notation evaluates to the previous working directory.

  5. The OLDPWD variable contains the previous working directory.

  6. The hyphen refers to the previous working directory; cd to go to the previous working directory and display the directory.

11.8.6 New ksh Metacharacters

The new Korn shell metacharacters are used for filename expansion in a way that is similar to the regular expression metacharacters of egrep and awk . The metacharacter preceding the characters enclosed in parentheses controls what the pattern matches. See Table 11.8.

Table 11.8. Regular Expression Wildcards

Regular Expression

Meaning

abc?(29)1

? matches zero or one occurrences of any pattern in the parentheses. The vertical bar represents an OR condition; for example, either 2 or 9 . Matches abc21 , abc91 , or abc1 .

abc*([0 “9])

* matches zero or more occurrences of any pattern in the parentheses. Matches abc followed by zero or more digits; for example, abc , abc1234 , abc3 , or abc2 .

abc+([0 “9])

+ matches one or more occurrences of any pattern in the parentheses. Matches abc followed by one or more digits; for example, abc3 or abc123 .

no@(onene)

@ matches exactly one occurrence of any pattern in the parentheses. Matches noone or none .

no!(thingwhere)

! matches all strings except those matched by any of the patterns in the parentheses. Matches no , nobody , or noone , but not nothing or nowhere .


Example 11.32.
 1   $  ls   abc abc1 abc122 abc123 abc2 file1 file1.bak file2 file2.bak none   nonsense noone nothing nowhere one  2   $  ls abc?(12)   abc  abc1 abc2  3   $  ls abc*([15])   abc   abc1  abc122 abc123 abc2  4   $  ls abc+([15])   abc1  abc122 abc123 abc2  5   $  ls no@(thingne)   none   nothing  6   $  ls no!(onensense)   none  nothing nowhere  

EXPLANATION

  1. All the files in the present working directory are listed.

  2. Matches filenames starting with abc and followed by zero characters or one of either of the patterns in parentheses. Matches abc , abc1 , or abc2 .

  3. Matches filenames starting with abc and followed by zero or more numbers between 1 and 5 . Matches abc , abc1 , abc122 , abc123 , and abc2 .

  4. Matches filenames starting with abc and followed by one or more numbers between 1 and 5 . Matches abc1 , abc122 , abc123 , and abc2 .

  5. Matches filenames starting with no and followed by exactly thing or ne . Matches nothing or none .

  6. Matches filenames starting with no and followed by anything except one or nsense . Matches none , nothing , and nowhere .

11.8.7 The noglob Variable

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 containing metacharacters in programs like grep , sed , or awk . If noglob is not set, all metacharacters must be escaped with a backslash if they are not to be interpreted.

Example 11.33.
 1   %  set -o noglob   # or set -f  2   %  print * ?? [] ~ $LOGNAME   * ?? [] /home/jody/ellie ellie  3   %  set +o noglob   # or set +f  

EXPLANATION

  1. The noglob variable is set. It turns off the special meaning of the wildcards for filename expansion. You can use the “f option to set the command to achieve the same results.

  2. The filename expansion metacharacters are displayed as themselves without any interpretation. Note that the tilde and the dollar sign are still expanded.

  3. The noglob option is reset. Filename metacharacters will be expanded.

 <  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