Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [88]

By Root 1003 0
7-6 (which assumes an ASCII environment) should explain things more clearly.

Table 7-6. Using the set construct wildcards

Expression

Single character matched

[abc]

a, b, or c

[.,;]

Period, comma, or semicolon

[-_]

Dash or underscore

[a-c]

a, b, or c

[a-z]

Any lowercase letter

[!0-9]

Any nondigit

[0-9!]

Any digit, or an exclamation mark

[a-zA-Z]

Any lower- or uppercase letter

[a-zA-Z0-9_-]

Any letter, any digit, underscore, or dash

In the original wildcard example, whizprog.[co] and whizprog.[a-z] both match whizprog.c and whizprog.o, but not whizprog.log.

An exclamation mark after the left bracket lets you "negate" a set. For example, [!.;] matches any character except period and semicolon; [!a-zA-Z] matches any character that isn't a letter.

The range notation is handy, but you shouldn't make too many assumptions about what characters are included in a range. It's generally safe to use a range for uppercase letters, lowercase letters, digits, or any subranges thereof (e.g., [f-q], [2-6]). Don't use ranges on punctuation characters or mixed-case letters: e.g., [a-Z] and [A-z] should not be trusted to include all of the letters and nothing more. The problem is that such ranges are not entirely portable between different types of computers.

Another problem is that modern systems support different locales, which are ways of describing how the local character set works. In most countries, the default locale's character set is different from that of plain ASCII. To solve these problems, the POSIX standard introduced bracket expressions to denote letters, digits, punctuation, and other kinds of characters in a portable fashion. We discussed bracket expressions in Section 3.2.1.1. The same elements that may appear in regular expression bracket expressions may also be used in shell wildcard patterns in POSIX-conformant shells, but should be avoided in portable shell scripts.

Hidden files

By convention, when doing wildcard expansion, Unix shells ignore files whose names begin with a dot. Such "dot files" are typically used as program configuration or startup files. Examples include $HOME/.profile for the shell, $HOME/.exrc for the ex/vi editor, and $HOME/.inputrc for the GNU readline library used by bash and gdb (among others).

To see such files, provide an explicit period in front of the pattern. For example:

echo .* Show hidden files

You may use the -a (show all) option to ls to make it include hidden files in its output:

$ ls -la

total 4525

drwxr-xr-x 39 tolstoy wheel 4096 Nov 19 14:44 .

drwxr-xr-x 17 root root 1024 Aug 26 15:56 ..

-rw------- 1 tolstoy wheel 32 Sep 9 17:14 .MCOP-random-seed

-rw------- 1 tolstoy wheel 306 Nov 18 22:52 .Xauthority

-rw-r--r-- 1 tolstoy wheel 142 Sep 19 1995 .Xdefaults

-rw-r--r-- 1 tolstoy wheel 767 Nov 18 16:20 .article

-rw-r--r-- 1 tolstoy wheel 158 Feb 14 2002 .aumixrc

-rw------- 1 tolstoy wheel 18828 Nov 19 11:35 .bash_history

...

* * *

Tip


We cannot emphasize enough that hiding dot files is only a convention. It is enforced entirely in user-level software: the kernel doesn't treat dot files any differently from any other files.

* * *

* * *

[4] Since files are kept within directories in an unspecified order, the shell sorts the results of each wildcard expansion. On some systems, the sorting is subject to an ordering that is appropriate to the system's location, but that is different from the underlying machine collating order. Unix traditionalists can use export LC_ALL=C to get the behavior they're used to. This was discussed earlier, in Section 2.8.

Command Substitution

Command substitution is the process by which the shell runs a command and replaces the command substitution with the output of the executed command. That sounds like a mouthful, but it's pretty straightforward in practice.

There are two forms for command substitution. The first form uses so-called backquotes, or grave accents (`...`), to enclose the command to be run:

for i in `cd

Return Main Page Previous Page Next Page

®Online Book Reader