Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [69]

By Root 949 0
and exits if count is undefined.

${ varname :+ word }

If varname exists and isn't null, return word; otherwise, return null.

Purpose: To test for the existence of a variable.

Example: ${count:+1} returns 1 (which could mean "true") if count is defined.

The colon (:) in each of the operators in Table 6-1 is optional. If the colon is omitted, then change "exists and isn't null" to "exists" in each definition; i.e., the operator tests for existence only.

The operators in Table 6-1 have been part of the Bourne shell for more than 20 years. POSIX standardized additional operators for doing pattern matching and text removal on variable values. The classic use for the new pattern-matching operators is in stripping off components of pathnames, such as directory prefixes and filename suffixes. With that in mind, besides listing the shell's pattern-matching operators, Table 6-2 also has examples showing how all of the operators work. For these examples, assume that the variable path has the value /home/tolstoy/mem/long.file.name.

* * *

Tip


The patterns used by the operators in Table 6-2 and in other places in the shell, such as the case statement, are all shell "wildcard" patterns. They're described in detail in Section 7.5. However we expect that you're familiar with the basics from your regular everyday use of the shell.

* * *

Table 6-2. Pattern-matching operators

Operator

Substitution

${ variable # pattern }

If the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest.

Example: ${path#/*/}

Result: tolstoy/mem/long.file.name

${ variable ## pattern }

If the pattern matches the beginning of the variable's value, delete the longest part that matches and return the rest.

Example: ${path##/*/}

Result: long.file.name

${ variable % pattern }

If the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest.

Example: ${path%.*}

Result: /home/tolstoy/mem/long.file

${ variable %% pattern }

If the pattern matches the end of the variable's value, delete the longest part that matches and return the rest.

Example: ${path%%.*}

Result: /home/tolstoy/mem/long

These can be hard to remember, so here's a handy mnemonic device: # matches the front because number signs precede numbers; % matches the rear because percent signs follow numbers. Another mnemonic comes from the typical placement (in the USA, anyway) of the # and % keys on the keyboard. Relative to each other, the # is on the left, and the % is on the right.

The two patterns used here are /*/, which matches anything between two slashes, and .*, which matches a dot followed by anything.

Finally, POSIX standardized the string-length operator: ${# variable } returns the length in characters of the value of $ variable:

$ x=supercalifragilisticexpialidocious

A famous word with amazing properties

$ echo There are ${#x} characters in $x

There are 34 characters in supercalifragilisticexpialidocious

Positional parameters

The so-called positional parameters represent a shell script's command-line arguments. They also represent a function's arguments within shell functions. Individual arguments are named by integer numbers. For historical reasons, you have to enclose the number in braces if it's greater than nine:

echo first arg is $1

echo tenth arg is ${10}

You can apply all of the value-testing and pattern-matching operators from the previous section to the positional parameters as well:

filename=${1:-/dev/tty} Use argument if given, /dev/tty if not

Special "variables" provide access to the total number of arguments that were passed, and to all the arguments at once:

$#

Provides the total number of arguments passed to the shell script or function. It is useful for creating loops (covered later in Section 6.4) to process options and arguments. For example:

while [ $# != 0 ] $# decremented by shift, loop will terminate

do

case $1 in

... Process first argument

Return Main Page Previous Page Next Page

®Online Book Reader