Coding for me is a pastime and learning experience. I'm not a dedicated coder or even a fulltime coder by any means. I don't commit to one language and relish learning "enough" about as many as I can. Learning coding in this way means I'll never be a software developer, or software engineer. I probably won't release any huge packages, programs or even make large contributions to major software projects. What I will do is become compitent to administer a variety of software for either myself or in a business setting. In this sense I am accomplishing one imprtant thing, independence. I rarely half to resort to getting help from others concerning anything either programming related or unix/linux related as well. I don't suggest this method for everyone. Someone who wants to actually write usefull mature applications would best be served by picking a language and sticking with it.
Bash or the Bourne Again Shell is not a programming language but a shell environment which has a built in scripting language. Bash is the default shell on most all linux's and is what you see in the console and in terminal emulation programs like xterm, rxvt, konsole et. al. Don't let this fool you as many many things can be done in bash scripting but we'll start with something very simple that can be used by everyone and that is the recreation of the .bat or batch file in dos. We can use bash to pass program flags to executables in much the same way that dos uses batch files for many application. The first thing we'll do is start an application with a common flag. I'm going to use the game quake 3 here in my example and well show a small bash script to start it with a command line to set a few switches of we'll be using. The file is called ioquake3.sh as we'll be starting the opensource version of quake 3 with this shell script.
#!/bin/sh cd "/usr/local/games/quake3" ./ioquake3.i386 +set sv_pure 0 +set vm_cgame 0 +set vm_game 0 +set vm_ui 0 $* exit $?
Lets talk about what this program is doing. The first thing you'll notice is ( #!/bin/bash ) . This is always the first line of bash scripts and tells the shell what command interpreter, in this case bash, to use when running this script. The second line you'll see is the command to cd (change directory) to where quake3 is installed, in this case /usr/local/games/quake3. The third line is the actual executable we'll run with the command line switches it rerquires for the shrared libs that ioquake3 requires. The $* at the end of the second line is a means of passing even further arguments to the executable when calling ioquake3.sh. Lastly we exit our shell script and output any passed instructions from the executable.
In essence we've strung a coupe of commands together in the correct directory to get a result we expect. We could have easily ran it this way from a console or terminal:
[dan@dtd:~]$ cd /usr/local/games/quake3/ [ENTER] [dan@dtd:quake3]$ ./ioquake3.i386 +set sv_pure 0 +set vm_cgame 0 +set vm_game 0 +set vm_ui 0 [ENTER]
If we place ioquake3.sh somewhere in our path, all that needs done is call the command ioquake3.sh , hit the enter key and our program wil run as intended. That not only saves a lot of typing but in this case we don't have to remember all those command line switches. Using bash scipts as batch files is easy and helpfull.
Firefox is a popular web browser thats light, customizable and displays html
properly and quickly. Need to have firefox delete it's cache files when you
close it? code:
#!/bin/bash /usr/bin/firefox $* || exit 1 rm -rf ~/.mozilla/firefox/YOUR-PROFILE.default/Cache/* exit $?
You must edit the third line with the correct path to your firefox profile and cache files. ~/ means your home directory on a unix/linux box.
Lets look at another shell script to solve a problem I had getting Pine to open links in the firefox web browser. I want any link I open from any other application, whether it be an email application or file manager, to open in a new tab within an already running firefox. If no firefox is running at the time, I would like a new firefox session to open displaying the link I have selected. Firefox has a command to open a passed $URL in a new tab, but what if firefox isn't running. Here's one solution:
#!/bin/bash url="$1" if [ "x$url" = "x" ]; then url="about:blank" fi if firefox.sh -remote openURL\("$url"\,new-tab\); then exit 0 fi exec firefox.sh "$url"