Java Reference
In-Depth Information
where myscript is the name of the file in which you have put your commands.
(See Chapter 2 for more on how to do that.) Once you've got a script running
the way you'd like, you might want to make its invocation as seamless as any
other command. To do that, change its permissions to include the execution
permission and then, if the file is located in a place that your PATH variable
knows 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's
a common place to put homebrew commands. Just be sure that $HOME/bin is
in 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 on which are given the first, second, and so on parameters on the
command line that you used to invoke your script. If you type myscript
Account.java then $1 will have the value Account.java for that invocation
of the script.
We don't have the space to go into all that we'd like to about shell pro-
gramming, but let us leave you with a simple example that can show you some
of its power. Used in shell scripts, for loops can take a lot of drudgery out of
file maintenance. Here's a simple but real example.
Imagine that your project has a naming convention that all Java files asso-
ciated with the user interface on your project will begin with the letters “UI”.
Now suppose your boss decides to change that convention to “GUI” but you've
already created 200 or more files using the old naming convention. Shell script
to the rescue:
for i in UI*.java
do
new="G${i}"
echo $i ' ==> ' $new
mv $i $new
done
You could just type those commands from the command line—that's the
nature of shell syntax. But putting them into a file lets you test out the script
without having to type it over and over, and keeps the correct syntax once
Search WWH ::




Custom Search