thoni56 thoni56 - 14 days ago 5
Bash Question

Datadriven shell programming? Suggestions, please

I'd like to convert my shell script to use a datadriven approach. But since there are no "tables" type values in any shell (that I know of), what are suggested alternative ways to do this?

What I'm looking for is solutions that would allow one to do things like:

animals = [['horse', 300 'brown'],
['cat', 3, 'black'],
['elephant', 3000, 'grey'],
['mouse', 0.3, 'grey']]
for a in animals ; do
echo "A typical $a[1] is $a[3] and weighs about $a[2] kilograms."
done


More precisely, I'd like to try a number of commands and see if one of them is available, and then send arguments to it:

commands = [['c1', '-m', '-i'],
['c2', '-message', '-icon'],
['c3', '/m', '/i']]
for c in commands ; do
if exists c[1] ; then
command = c[1]
message_option = c[2]
icon_option = c[3]
break;
fi
done
$command $message_option "message" $icon_option icon

Answer

You can define and use associative-arrays in bash for your requirement.

#!/bin/bash

# declaring the Associative array
declare -a animals

animals[0]="'horse':300:'brown'"
animals[1]="'cat':3:'black'"
animals[2]="'elephant':3000:'grey'"
animals[3]="'mouse':0.3:'grey'"

for animal in "${animals[@]}"
do
    myArray=(${animal//:/ })
    printf "A typical "${myArray[0]}" is "${myArray[2]}" and weighs about "${myArray[1]}" kilograms.\n"
done

The only tricky part in the above is parameter-expansion of type

${parameter/pattern/string}
          The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. 
If pattern begins with ‘/’, all matches of pattern are replaced with string. Normally only the first match is replaced. 

So ideally the string 'horse':300:'brown' is split as individual elements and stored in the array myArray which is later used to access individual elements in a C-style loop.