Friday, July 22, 2005

bash scripting

Write a bash script and use it from any directory

I wrote up a simple bash script that automates creating a .java file template the way I like my java programs laid out. What I wanted to do was be able to use this bash script from any directory. From my /home/username directory I had created another directory /home/username/scripts that is intended to house any scripts I write up.

It is simple enough to write a bash script and access it from any directory via

> bash /home/username/scripts/theScript args

but I wanted something simpler. Something like

> theScript args

And here's how it's done. You have written a script (theScript) and placed it in your /home/username/scripts directory. Now change the permission of the script

> chmod +x theScript

which allows you to execute the script. Now open up your .bashrc from /home/username/.bashrc and add the following line to the end of it.

export PATH=$PATH:/home/username/scripts

then save the .bashrc and close it. The changes have now been applied, but you must open a new terminal for it to take effect in that terminal.

-Getting better at this

No comments:

Followers