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.

22Chapter 1An Embarrassment of Riches: The <strong>Linux</strong> Envir<strong>on</strong>mentEntire books can be—and have been—written <strong>on</strong> shell programming.(It’s <strong>on</strong>e of our favorite subjects to teach.) Programs written in the shell languageare often called shell scripts. Such scripts can be powerful yet easy to write(<strong>on</strong>ce you are familiar with the syntax) and can make you very productive indealing with all those little housekeeping tasks that accompany programdevelopment. All you need to do (dangerous words, no?) is to put commandsin a text file and give the file execute permissi<strong>on</strong>s. But that’s a subject foranother day.Some elements of shell scripting, however, are useful even if you nevercreate a single shell script. Of these, perhaps the most important to know(especially for Java programmers) is how to deal with shell variables.NOTEWe’ll be describing the syntax for bash, the default shell <strong>on</strong> most <strong>Linux</strong>distributi<strong>on</strong>s. The syntax will differ for other shells, but the c<strong>on</strong>cepts are largelythe same.Any string of alphanumeric or underscore characters can be used as thename of a variable. By c<strong>on</strong>venti<strong>on</strong> shell variables typically use uppercasenames—but that is <strong>on</strong>ly c<strong>on</strong>venti<strong>on</strong> (although it will hold true for most ifnot all of our examples, too). Since commands in <strong>Linux</strong> are almost alwayslowercase, the use of uppercase for shell variables helps them to stand out.Set the value of a shell variable with the familiar method—the equal sign:$ FILE=/tmp/abc.out$This has assigned the variable FILE the value /tmp/abc.out. But to makeuse of the value that is now in FILE, the shell uses syntax that might not befamiliar to you: The name must be preceded with a “$”.Shell variables can be passed <strong>on</strong> to other envir<strong>on</strong>ments if they are exported,but they can never be passed back up. To set a shell variable for use by yourcurrent shell and every subsequent subshell, export the variable:$ export FILE$

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

Saved successfully!

Ooh no, something went wrong!