[ale] how to do named hash in bash

Charles Shapiro hooterpincher at gmail.com
Mon Mar 31 12:26:04 EDT 2008


Well hmm.  Bash supports arrays, but I don't believe it supports
dictionaries directly.

That said, with unlimited time and money anything is possible. I've done
stuff like this when necessary with for loops..

#!/bin/bash

nv_pairs="one_1 two_2 three_3 four_4"

get_name_for_value () {
   for kk in $nv_pairs
   do
      name=`echo $kk | awk -F_ '{print $1}'`
      if [ $name == $1 ]
      then
         value=`echo $kk | awk -F_ '{print $2}'`
      fi
   done
   echo $value
}

Name=three
val=`get_name_for_value $Name`
echo Name: $Name Value: $val


On 3/31/08, Jerry Yu <jjj863 at gmail.com> wrote:
>
> I  need to do named has in bash scripting, the equivalent in perl, "my
> $hash{$key} = $value;". Can I?
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.ale.org/pipermail/ale/attachments/20080331/63461321/attachment.html 


More information about the Ale mailing list