Bash Dictionary – Terrible Hack of the Day

So, I just had a reason to implement something like a dictionary in Bash.  Of course, I could try to determine if such a feature exists already (it does in Bash 4, alas the default on OS X seems to be 3.2), but why not just try to hack something together?

Here’s what I got initially:

OFFSETDICTkeyUno=5
OFFSETDICTkeyDos=4
OFFSETDICTkeyTres=17
OFFSETDICTkeyQuatro=21
keyToLookup="keyQuatro"
value="$(eval echo '$OFFSETDICT'${keyToLookup})"
echo $value
>     21

So, that’s ridiculous.  Basically, the values are set in code with the dictionary name “OFFSETDICT”, and the key as whatever comes after the name {keyUno, keyTres, …}.  Then the value line builds a variable lookup and executes it.  That’s pretty lazy of me to write it like that, so here’s a function (a Bash function – so terrible).

function get {
     dictName="$1"
     key="$2"
     CMD='$'"${dictName}${key}"
     echo $(eval echo "$CMD")
}
a=`get OFFSETDICT keyQuatro`
echo $a
>     21

Hrm, I wrote a getter, I should probably make a setter too.

function set {
     dictName="$1"
     keyName="$2"
     value="$3"
     CMD="${dictName}${keyName}"='"'"$value"'"'
     eval "$CMD"
}
get OFFSETDICT keyCinco
>     
set OFFSETDICT keyCinco "Sup my man?"
get OFFSETDICT keyCinco
>     Sup my man?

You can imagine what this does to the variable space.  But if you’re really worried about that, maybe don’t use Bash.

I really just needed to look up one value based on another value.  I guess an array would have worked, but that’s not much of a stupid hack.

Meteor Shower

The back of a truck is an appropriate place to watch a meteor shower.  I had driven out onto a back road in Yellow Springs, Ohio, to watch the meteors fall, and I was not disappointed.  Orion knew where most of them were coming from – he was calling the shots.

I had barely pulled off the two-lane dirt road and onto some tire tracks in the grass.  A run-down barbed wire fence with old knotty posts ran alongside the road, and was only a few feet from the truck.  Fortunately, some farmer had left room for me.

In this part of the US the sky is never truly dark.  Even on this cloudless night I could see Xenia’s light, about 10 miles away, low and dim on the horizon.  The light from Cincinnati, another 30 mi past Xenia, was clearly visible in the South.

Radio towers blinked on the horizon.  I could count perhaps 20, warning the aircraft flying overhead.  And there were 10 or more aircraft overhead at any given time.  It’s easy to count aircraft with such a big sky.  The open fields surrounding me had few trees, and then only at a distance.

A car passed my truck as I lay in the bed.  I saw it approach through the cab windows, and at the same time the grass over my shoulder rustled.  I covered my eyes to try to maintain my night vision.  A house cat or something was moving through the grass, a short distance from the fence.

The car’s headlight was blinding now, as it finally passed me.  The shush of its tires on the dirt was deafening in the country-night silence.

A trick of the headlights caught the corner of my eye, and I noticed a misshapen old fencepost not far from the truck.  It had been placed only a few feet away from the neighboring posts.  Perhaps the farmer had replaced it, but decided not to dig it out.

The meteor shower picked up intensity overhead.  They fell almost two every ten seconds.  One large meteor burned out quickly, and only covered a small portion of the sky.  Another broke into two, burning across the sky together.  A third meteor was dim enough to nearly miss, but left a streak across half the sky.  It must have only skimmed the atmosphere.

Again, the fencepost caught the corner of my eye.  It seemed to change shape slightly.  The rest of my eye was drawn to it, and I craned my head to look properly.

Clearly now I watched as the fencepost moved, taking the shape of a head atop shoulders.  A figure turned and moved towards me from where the fencepost had been standing.

I recoiled in shock, and slammed my head on the edge of the truck bed.  Trying to scramble to my feet, I found that my leg had fallen asleep, and was useless.  As I leaned on the side wall to stand, I tumbled over and into the dirt near the driver’s side door.

The figure had closed on me, and was reaching out to me, as I lay in the dirt.  I couldn’t find the keys in my pocket, somehow it was filled only with old receipts and gum wrappers.  I pushed myself into a sitting position, and kicked back, slamming into the tire well.

The being was upon me now.  He grabbed my arm – I couldn’t move.  There was no room even for horror.

“Could you give me a ride?”