Text Link Ads

Tuesday, October 16, 2007

Tip for the day: BOURNE SHELL PSEUDO HASH TABLES

The best way to explain this is with an example:

You may want to write a simple script that launches an xterm window with different color
options. Pseudo hash tables in bourne shell.

$ xwindow MidnightBlue

An easy way to this is by first listing the supported colors in the script (pseudo hash table):

DarkSlateGrey="#2f4f4f"
DimGrey="#696969"
MidnightBlue="#191970"
NavyBlue="#000080"

Next, simply grab the hex color code by using the following command:

BGColor=`eval echo $"$(echo $1)"`

"$(echo $1)" evaluates to "MidnightBlue" and
`eval echo $MidnightBlue evaluates to "#191970".

Simply launch the xterm window using $BGColor as the background color:

xterm -bg="$BGColor" &

This method for extracting pseudo hash values may be used for NIS map files, file date stamps, GUI colors, etc.

1 comment:

jgarneau said...

This was really helpful, but sh can be pretty persnickety about the syntax here. I found that

value=`eval echo "\\$$(echo $key)"`

works better for reading a value.
changing a value is simple:

eval `echo $key`="newvalue"