13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

14Chapter 1An Embarrassment of Riches: The <strong>Linux</strong> Envir<strong>on</strong>mentSec<strong>on</strong>d, the pattern matching is d<strong>on</strong>e by the shell, the command interpreter,before the arguments are handed off to the specific command. Any textwith these special characters is replaced, by the shell, with <strong>on</strong>e or more filenamesthat match the pattern. This means that all the other <strong>Linux</strong> commands (mv,cp, ls, and so <strong>on</strong>) never see the special characters—they d<strong>on</strong>’t do the patternmatching, the shell does. The shell just hands them a list of filenames.The significance here is that this functi<strong>on</strong>ality is available to any and everycommand, including shell scripts and Java programs that you write, with noextra effort <strong>on</strong> your part. It also means that the syntax for specifying multiplefiles doesn’t change between commands—since the commands d<strong>on</strong>’t implementthat syntax; it’s all taken care of in the shell before they ever see it. Any commandthat can handle multiple filenames <strong>on</strong> a command line can benefit fromthis shell feature.If you’re familiar with MS-DOS commands, c<strong>on</strong>sider the way patternmatching works (or doesn’t work) there. The limited pattern matching youhave available for a dir command in MS-DOS doesn’t work with other commands—unlessthe programmer who wrote that command also implementedthe same pattern matching feature.What are the other special characters for pattern matching with filenames?Two other c<strong>on</strong>structs worth knowing are the questi<strong>on</strong> mark and the squarebrackets. The “?” will match any single character.The [...] c<strong>on</strong>struct is a bit more complicated. In its simplest form, itmatches any of the characters inside; for example, [abc] matches any of a orb or c. So Versi<strong>on</strong>[123].java would match a file called Versi<strong>on</strong>2.javabut not those called Versi<strong>on</strong>12.java or Versi<strong>on</strong>C.java. The patternVersi<strong>on</strong>*.java would match all of those. The pattern Versi<strong>on</strong>?.java wouldmatch all except Versi<strong>on</strong>12.java, since it has two characters where the ?matches <strong>on</strong>ly <strong>on</strong>e.The brackets can also match a range of characters, as in [a-z] or [0-9].If the first character inside the brackets is a “^” or a “!”, then (think “not”) themeaning is reversed, and it will match anything but those characters. SoVersi<strong>on</strong>[^0-9].java will match Versi<strong>on</strong>C.java but not Versi<strong>on</strong>1.java.How would you match a “-”, without it being taken to mean a range? Put itfirst inside the brackets. How would you match a “^” or “!” without it beingunderstood as the “not”? D<strong>on</strong>’t put it first.Some sequences are so comm<strong>on</strong> that a shorthand syntax is included. Someother sequences are not sequential characters and are not easily expressed as arange, so a shorthand is included for those, too. The syntax for these special

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!