Thursday, July 10, 2008

Bash Script to open SD Card folder in File Manager

As mentioned earlier in the post on my Easy Mode GUI, I have an entry that goes straight to my SD Card folder in File Manager. Instead of a hard absolute link, eg. /home/user/MMC-SD/Kingston, I wrote a bash script to search the MMC-SD folder and open the first (and only) folder in File Manager.

I love programming and take it as my hobby. Not as a job though :P I started tinkering with computers in Secondary 1 after my uncle gave me an XT computer after my PSLE. I went to Informatics to pick up QBASIC when I was in Secondary 2, and it went on from there.
Sec 3: C
JC: Pascal, FoxPro, HTML
University: Scheme (a dialect of Lisp), Java, SQL, ASP, PHP, Prolog, SML

It's a lot of fun and satisfaction when the program gets running, so doing up all these bash scripts, though simple, are quite fulfilling for me too :)

A bash script in Linux (or UNIX) is basically like a batch file (.bat) in DOS. In a bash script, comments are preceded by #, except for #!/bin/bash. Assigning values to variables are done without the $ sign and there musn't be any spaces in between (a=2 correct, a = 2 wrong). This had me running in circles for quite a while. References to variables are done with the $ sign.

For the 'for' loop, there's no need for a semi-colon if the 'do' is not on the same line. Instead of letting the loop run forever, I inserted a break so that the loop will just run one time. This is assuming that there is one and only one folder in MMC-SD. The steps to creating the bash script are below.

  1. Press Ctrl-Alt-T to open the terminal console.

  2. Type "sudo nano /usr/bin/openSDCard" to create a bash script in /usr/bin. Nearly all bash scripts are placed there as it is in $PATH and therefore can be executed anywhere.

  3. Type the following:

    #!/bin/bash

    DIRECTORY="/home/user/MMC-SD"

    for file in $DIRECTORY/*
    do
    #for variable assignment, there cannot be spaces in between
    firstdir=$file
    break
    done

    /usr/bin/XandrosFileManager -caption File\ Manager -maximized $firstdir

    exit 0

  4. Press Ctrl-O to save and Ctrl-X to exit.

  5. Type "sudo chmod 755 /usr/bin/openSDCard" to set the correct permissions to run.

  6. To run, just type "openSDCard".

(^ v ^)

No comments: