07 January 2008

Quick Scripting in Linux

I recently saw a post on Lifehacker.com, which pointed to the article on Inspect My Gadget.com, about how to use batch files to easily setup work spaces on Windows. I thought that was a great time saving idea, and could be easily done on my Linux computer. I will let you read the original post for the why, but I will describe the how below. Let me tell you that I setup three scripts for my own use:

  • Blog – it loads Flock, Amarok, a file manager, and a text editor

  • Evening – Kontact (testing it for a todo manager), Thunderbird (offline), Firefox (it normally loads two tiddlywikis), my check register, and my personal test tracking sheet.

  • Morning – Firefox (same two tiddlywikis), and Kontact

Morning and evening are set to automatically execute at certain hours of the day, very handy.


Housekeeping please article

The first thing that I do when I'm playing with something new on my computer, is make sure that I have a special folder for that project. It keeps things nice and clean, after all, I don't want my desktop to become the junk drawer of my computer. While I was working on this project I created a folder called Scripts in my home folder; I later moved the scripts to my “bin” folder, for easier execution.


Scripts (Actually called Shell Scripts)

Scripts are simply a list of commands that you would normally type into the shell. Scripting is very powerful, and can harm your system if not done correctly – consider yourself warned. I'm only going to cover an extremely small percentage of what can be done with scripts; I'm writing a blog, not a book.


Writing the script

Writing the script is rather easy. The first thing you want to do is pull up your favorite text editor, anything that can save in plain text is okay. Next type in the program name (ie. firefox), or the path to the program (ie. /usr/bin/firefox or ~/Programs/firefox/firefox), along with any options you might want to add. Repeat with a new line for each program you wish to execute, or document you want to open.


Example Script:

#Kontact
kontact &
#Thunderbird
~/Programs/thunderbird/thunderbird -offline &
#Firefox
~/Programs/firefox/firefox -P default lifehacker.com google.com &
#OpenOffice
ooffice -calc “Documents/Personal Tests/Personal Test.ods” &
ooffice -calc ~/Documents/Budget/Check\ Register.ods &

Let me quickly explain what is going on in this script.

#Kontact
This line is a comment. Anything that is prefaced with a hash mark (#) is ignored by the shell, so you can write any comments you want after.

kontact &
This is a fairly simple command. I typed in the program name kontact, no need for me to tell the computer where to find it, it already knows.

#Thunderbird
The comment again, it's just good practice.

~/Programs/thunderbird/thunderbird -offline &
Again, a rather simple command. I simply typed in the path of the program I wanted to load ~/Programs/thunderbird/thunderbird. I have this program stored in my home directory, so I wasn't able to simply type in the program name, as I did with Kontact (which is installed properly). I also told Thunderbird to startup in offline mode -offline.


#Firefox
Comment, yet again.


~/Programs/firefox/firefox -P default lifehacker.com google.com &
This line has a few things going on. The first being that I wrote out the location of the program that I wanted to execute ~/Programs/firefox/firefox, just like with Thunderbird above. Next I told Firefox that I want to use the profile called “default” -P default, the -P tells Firefox to use the profile that you specify. Last but not least, I specified some URLs for Firefox to load lifehacker.com google.com, note that any tabs you had opened in a previous Firefox session will most likely open as well.


#OpenOffice
One comment to rule the next two. Since I'm launching two instances of the same program, I only needed to comment once.


ooffice -calc “Documents/Personal Tests/Personal Test.ods” &
In this line I'm opening OpenOffice ooffice and telling it that I want to use the spreadsheet -calc. Next I told OpenOffice that I wanted to open the spreadsheet “Personal Test” “Documents/Personal Tests/Personal Test.ods” (note the quotes around the document path).


ooffice -calc ~/Documents/Budget/Check\ Register.ods &
As in the previous line, I'm starting the OpenOffice spreadsheet program ooffice -calc, and telling it to open a specific document. I did not use the quotes in this line, instead choosing to write-out where the document is in greater detail (which is sometimes needed). First I told OpenOffice to start in the current users home directory ~, then I told it where in the home folder to find the file /Documents/Budget/Check\ Register.ods. If you do not use quotes, and you have a space anywhere in the document path or filename, then you will want to put a backslash (\) before the space; the backslash tells the computer that the space is part of the document name, rather than the start of another variable.


Just a couple things of note from the previous section. You saw the ampersand (&) at the end of each line, this tells the computer to run the command in the background; I have not found the ampersand to be needed, but it can sometimes make things run a bit smoother. I somewhat explained the tilde (~) above, let me explain a bit more. The is a shortcut, indicating the current users home directory [~ = /home/user], it can save a lot of typing; for this kind of scripting you most likely won't need it, but it can come in handy.


Execute him
Once you have the text file all written out, and stored where you want it (see Housekeeping above), you will want to tell the computer that it is an executable, instead of a simple text document. You can do this a multiple ways, I will show you two.

Command Line
The easiest way to make a file executable from the command line is to navigate to the file, and type in chmod +x [file name], where [file name] is the scripts name.

Don't go getting GUI on me
The most visual method is to use the graphical interface. In the top two Linux environments, Gnome and KDE, the process is almost exactly the same. Bring up your file browser, navigate to the folder that holds your script. Once you are in the folder, right click on the script, select “Properties”, click on the “Permissions” tab, and check the “is executable” or “Allow executing file as program” box; click OK, and you are done.

You can now execute the script from the command line, or create a link to it, and make it an easy click on your desktop, or menu.

http://lifehacker.com/337353/set-up-instant-working-environments-with-batch-files
http://www.inspectmygadget.com/2007/12/19/how-to-use-batch-files-to-create-a-working-environment/


I'm using Flock's built-in blog posting ability for the first time with this post, so I hope it goes okay.

No comments: