Thursday, June 30, 2011

Associative Arrays in Bash 4

 John Shepard, one of Chariot's architects and newest bloggers, just recently posted this on his blog.
 A little different from our more focused software dev posts, we hope you will find this information useful.


I recently switched my work machine from OS X to Fedora. One of the first programs I noticed was missing was Time Machine. After being unable to get any of the backup software for Linux to behave as desired, a project emerged.

Ignoring most of the details of the backup scripts (all the scripts are available at the end of this post), I'm going to focus on the part working with associative arrays.

The backup program would need some way to keep only one backup per day for previous days. The obvious way to determine which days are affected is to keep a key value pair of dates and backup counts per day. Key value pairs have a few different names such as maps or dictionaries. In bash key value pairs are called associative arrays. 


To declare an associative array use -A
declare -A MY_VARIABLE

To access the value just reference the variable as an array element
KEY="some value"
MY_VARIABLE["${KEY}"]

To access the value, or use zero if there is no value, use a default value
${MY_VARIABLE["${KEY}"]:-0}

And to set a value (here the value for the key is being set to two)
MY_VARIABLE["${KEY}"]=2

For looping, to access all keys
${!MY_VARIABLE[@]}

The entire set of backup scripts is available on github. cleanup_day.sh contains the associative array code.

Tuesday, June 21, 2011

HTML 5 Offline Applications with JQuery Mobile

I can't believe it is a month since our last post.  Vacation got in the way, but our consultants have been blogging on various topics.  There is some catching up to do over the next week.

New to the blogsphere is John Shepard.  John recently posted around JQuery ad HTML5, and what he has been doing with his spare time.


I was recently playing around with JQuery Mobile and was wondering how easy it would be to create an HTML 5 offline application. I'm not very familiar with HTML or JavaScript, so I figured this would be an interesting project.

A simple application was required, so a calculator was semi-randomly selected. (The source for the calculator is available at the end of this post.)

The first step in creating the calculator was to add some HTML that had a text area and some buttons. jQuery Mobile handles a resizing display area and can put buttons in a grid. The hooks into jQuery Mobile seem to be through data-roles and classes. Putting the textarea ....read more