Pattern Matching Operators

You can use substitution operators, as discussed in the preceding section, to do something if a variable does not have a value. You can use a pattern-matching operator to search for a pattern in a variable and, if that pattern exists, do something to the variable. This can be useful, because it allows you to tune a variable to be exactly the way you want it. Think, for example, of the situation where a user enters a complete path name of a file, but your script needs only the name of the file without the entire path. The pattern-matching operator is the way to change that; pattern-matching operators allow you to remove part of a variable automatically. In Listing 27-12 you see an example of a script that works with pattern-matching operators.

Listing 27-12. Working with Pattern-Matching Operators

# script that extracts the filename from a filename that includes the complete path

# usage: stripit <complete filename>

echo "The name of the file is $filename"

When executed, the script will show the following result:

[email protected] %> ./stripit /bin/bash the name of the file is bash

Pattern-matching operators always try to locate a given string. In this case, the string is */. In other words, the pattern-matching operator searches for a / preceded by something. In this pattern-matching operator, ## is used to search for the longest match of the string provided, starting from the beginning of the string. In other words, the pattern-matching operator searches for the last / that occurs in the string and removes it and everything that stands before the / as well. You can prove this by running the script with / bin/bash/ as an argument; in this case, the pattern that is searched for is on the last position of the string, and the pattern-matching operator removes everything.

The previous example explains how to use the pattern-matching operator that looks for the longest match. By using a single hash sign, you can let the pattern-matching operator look for the shortest match, again starting from the beginning of the string. If, for example, the script in Listing 27-12 uses filename=${1#/}, the pattern-matching operator would look for the first / in the complete filename and remove that.

In the previous example, you saw how to use pattern-matching to start searching from the beginning of a string. You can start searching from the end of the string as well. In this case, you use % instead of #; % refers to the shortest match of the pattern, and %% refers to its longest match. The script in Listing 27-13 shows how this works.

Listing 27-13. Using Pattern-Matching Operators to Start Searching at the End ofa String

# script that isolates the directoryname from a complete filename

# usage: stripdir <complete filename>

echo "The directory name is $dirname"

While executing, you'll see that this script has a problem:

[email protected] %> ./stripdir /bin/bash The directory name is

As you can see, the script does its work somewhat too enthusiastically and removes everything. Fortunately, you can solve this problem by first using a pattern-matching operator that removes the slash from the start of the complete filename (but only if that slash is provided) and removes everything following the first slash in the complete filename. The example in Listing 27-14 shows how this is done.

Listing 27-14. Fixing the Example from Listing27-13

# script that isolates the directoryname from a complete filename

# usage: stripdir <complete filename>

echo "The directory name is $dirname"

As you can see, the solution to the problem is to use ${#/}. This construction starts searching from the beginning of the filename to a /. Since no * is used here, it will look only for a / at the first position of the filename. If it finds one, it will remove the slash. So if a user enters usr/bin/passwd instead of /usr/bin/passwd, the ${#/} construction will do nothing. In the line after that, the variable dirname is defined again to do its work on the result of its first definition in the previous line. This line does the real job and looks for the pattern /*, starting at the end of the filename. This makes sure everything after the first slash in the filename is removed, and only the name of the top-level directory is echoed. Of course, you can tune this script easily to display the complete path where the file is in; just use dirname=${dirname%/*} instead.

Was this article helpful?

0 0

Post a comment