Friday, May 28, 2010

First attempts of bash scripting


I am quite new to linux, but so far I have noticed that the terminal that comes with it is quite powerfull. Realising that I will often use this console I decided to give my hand in bash scripting for the set of commands I will often use.


I came across this website that gave tutorials about bash scripting to relatively new users such as myself. The first test ofcourse is the ever popular "Hello World" script, it displays those legendary words. So I fired up vim and started typing away.


The script went like so :


  #!/bin/bash
  #declare STRING variable
  STRING="Hello World"
  #print variable on a screen
  echo $STRING
 

My first bash script was completed! Now I had to run it, this consisted of two commands I had to run in terminal. Please note that the "#" symbol usually is used for comments but "!#" also know as a shebang which is a place where you put your interpreter.


  chmod +x hello_world.sh
 

which made the bashscript executable and


  ./hello_word.sh
 

To actually run it


hello_word.sh being the bashscript file I just saved. After that produced the text "Hello World" in terminal, the cogs inside my brain started to turn and instantly wanted to try something out on my own, even though I was only in the first part of my tutorial. What I wanted to do is a script is launch google chrome(chromium browser in ubuntu) with 3 sites that I usually use of google. (If you think this product placement, please ring up google and tell me to pay me)


So my first attempt attempt of the script went like so


  chromium-browser -new-window www.igoogle.com www.gmail.com docs.google.com
 

I saved the script and ran it and vuola a new chrome window popped up with tabs of the corresponding urls. But I wasn't happy with just that I also wanted to output what tabs I am going to access before launching the browser, so that in future modifications I would always know what was supposed to happen, from what did happen. Since I only had 3 urls I tried placing them in variables and them outputting them using an echo, using the same variables for the command that launches the browser. My code went as follows:

  IGOOGLE="www.igoogle.com"
  GMAIL="www.gmail.com"
  DOCS="docs.google.com"
  echo "Opening the following pages in new chromium window :" $IGOOGLE $GMAIL $DOCS
  chromium-browser -new-window $IGOOGLE $GMAIL $DOCS
 

With the ammendments now it displays the text "Opening the following pages in new chromium window : www.igoogle.com www.gmail.com docs.google.com" before launching the browser with those urls. Now please note when declaring variables be sure that the "=" sign is right next to the variable name, otherwise the terminal would think it is a command. To use the variables as you've noticed you would need to place a "$" sign before them.

After I was proud of my fancy script, my laziness kicked in and said "What if in the future you need to add or remove urls, you would have to keep on declaring/removing variables and add/remove them from the echo and chrome commands". That was a no no for me, so scowered the internet to find out how to create an array, and I found a rather interesting way.

  GOOGLELIST="www.igoogle.com www.gmail.com docs.google.com"
  echo "Opening following pages in  new chromium window:"
  for SITE in $GOOGLELIST;
  do
   echo $SITE;
  done;
  chromium-browser -new-window $GOOGLELIST
 

Apparently I just had to use the normal syntax as declaring a variable, and the space between text is used as a way to device the information into array segments. After that I looped through the array to display the urls in the "array", but the great thing is that I didn't need to do it for the chromium-browser command since essentially it is still a string with the same format the browser required it to.


Now that I am happy with my bash script, I decided to give it an alias since I plan to use it quite often, and would be a pain to keep on going to it's directory to run it.


  alias kiwigoogle='~/mybashscriptdirectory/./kiwigoogle.sh'
 

So in terminal I typed the command alias followed by the keyword I want to associate with my script and set it to the absolute path of where my script is located. By giving the physical path, I could run this command from anywhere.


A reboot later I realised the my beloved alias was gone! This confused me at first but after a bit of research I learned that the set alias command needs to be saved in the .bash_aliases file found in the root folder.


If this didn't work for you access .bashrc that is also found in your root folder and see if the following lines of code are not commented


  if [ -f ~/.bash_aliases ]; then
      . ~/.bash_aliases
  fif