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.

1.3 Basic <strong>Linux</strong> C<strong>on</strong>cepts and Commands25where myscript is the name of the file in which you have put your commands.(See Chapter 2 for more <strong>on</strong> how to do that.) Once you’ve got a script runningthe way you’d like, you might want to make its invocati<strong>on</strong> as seamless as anyother command. To do that, change its permissi<strong>on</strong>s to include the executi<strong>on</strong>permissi<strong>on</strong> and then, if the file is located in a place that your PATH variableknows about, it will run as a command. Here’s an example:$ chmod a+rx myscript$ mv myscript ${HOME}/bin$ myscript... (script runs)$The file was put into the bin directory off of the home directory. That’sa comm<strong>on</strong> place to put homebrew commands. Just be sure that $HOME/bin isin your PATH, or edit .bashrc and add it.If you want to parameterize your shell, you’ll want to use the variables $1,$2, and so <strong>on</strong> which are given the first, sec<strong>on</strong>d, and so <strong>on</strong> parameters <strong>on</strong> thecommand line that you used to invoke your script. If you type myscriptAccount.java then $1 will have the value Account.java for that invocati<strong>on</strong>of the script.We d<strong>on</strong>’t have the space to go into all that we’d like to about shell programming,but let us leave you with a simple example that can show you someof its power. Used in shell scripts, for loops can take a lot of drudgery out offile maintenance. Here’s a simple but real example.Imagine that your project has a naming c<strong>on</strong>venti<strong>on</strong> that all Java files associatedwith the user interface <strong>on</strong> your project will begin with the letters “UI”.Now suppose your boss decides to change that c<strong>on</strong>venti<strong>on</strong> to “GUI” but you’vealready created 200 or more files using the old naming c<strong>on</strong>venti<strong>on</strong>. Shell scriptto the rescue:for i in UI*.javad<strong>on</strong>ew="G${i}"echo $i ' ==> ' $newmv $i $newd<strong>on</strong>eYou could just type those commands from the command line—that’s thenature of shell syntax. But putting them into a file lets you test out the scriptwithout having to type it over and over, and keeps the correct syntax <strong>on</strong>ce

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

Saved successfully!

Ooh no, something went wrong!