May 20, 2009

Effective way to use bash scripts to run command line applications

Effective way of using bash scripts to start command line applications

Due to the fact that I use the eee PC as my means getting Linux portable, I often had to figure out ways to minimise memory and harddisk usage, so as to speed things up even more. So in this quest, I stumbled into the world of "command line Linux". It seems a little unnerving to me at first, as I considered myself an average Linux desktop user, who never ventured into DOS even back in those days when I used Windoze. But I googled and figure out a way to use command line applications, with less repetitive typing. In short, I use bash scripts.

What I do is I write a bash scripts that run a certain simple application, and I then I put the bash script as an entry in my desktop menu ( I run icewm, by the way). Just to illustrate how to do it, for other beginners. This is how I do it.

Below is a very simple bash script to connect my pc to my home network. Of course, the details of the encryption key and ESSID is fake and arbitrary. And "ath0" is my network device.

#!/bin/bash
sudo ifconfig ath0 down
sudo iwconfig ath0 essid lala key 0123456789
sudo ifconfig ath0 up
sudo dhclient ath0

So I created the bash script and store the file as /home/user/homeconnect.sh, you can of course store it anywhere. Then I have to make the script executable by going to /home/user directory and "sudo chmod +x homeconnect.sh"

Then I go to "~/.icewm/menu". That's where the icewm menu settings can be made. I save the menu after I add in this new line "prog 'homeconnect.sh' icon xterm -e /home/user/homeconnect.sh". I don't need an icon for this entry so the "icon" term is just arbitrary.

So that's done. When I right-click my desktop, I'll see an entry "homeconnect.sh", and when I clicked it and it autoruns the script and connects me to my home network.

Pros
1) Command line apps will use less resources then GUI apps.
2) Learn more about bash scripts and how to use the command line more effectively.
3) Uber-geekiness factor increases. Haha.

Cons
1) It takes a certain balls to try it first for a beginner.

Of course, the bash script is too simple. I have progressed and now my script allows me to key in ESSID and encryption keys to log into other familiar networks and even lists out the networks in the area for reference. Trick is "sudo iwlist ath0 scan | grep -e ESSID -e key -e Quality".
Have fun with this howto and maybe you can share your bash scripts for other tasks that you do. =)

Click Here!