| 1 | #!/bin/bash |
| 2 | # $Id: ha.sh,v 1.2 2005/04/21 23:24:26 oliver Exp $ |
| 3 | # Copyright 2005 Oliver Beckstein |
| 4 | # Released under the GNU Public License |
| 5 | # Author of script granted permission for inclusion in ABS Guide. |
| 6 | # (Thank you!) |
| 7 | |
| 8 | #---------------------------------------------------------------- |
| 9 | # pseudo hash based on indirect parameter expansion |
| 10 | # API: access through functions: |
| 11 | # |
| 12 | # create the hash: |
| 13 | # |
| 14 | # newhash Lovers |
| 15 | # |
| 16 | # add entries (note single quotes for spaces) |
| 17 | # |
| 18 | # addhash Lovers Tristan Isolde |
| 19 | # addhash Lovers 'Romeo Montague' 'Juliet Capulet' |
| 20 | # |
| 21 | # access value by key |
| 22 | # |
| 23 | # gethash Lovers Tristan ----> Isolde |
| 24 | # |
| 25 | # show all keys |
| 26 | # |
| 27 | # keyshash Lovers ----> 'Tristan' 'Romeo Montague' |
| 28 | # |
| 29 | # |
| 30 | # Convention: instead of perls' foo{bar} = boing' syntax, |
| 31 | # use |
| 32 | # '_foo_bar=boing' (two underscores, no spaces) |
| 33 | # |
| 34 | # 1) store key in _NAME_keys[] |
| 35 | # 2) store value in _NAME_values[] using the same integer index |
| 36 | # The integer index for the last entry is _NAME_ptr |
| 37 | # |
| 38 | # NOTE: No error or sanity checks, just bare bones. |
| 39 | |
| 40 | |
| 41 | function _inihash () { |
| 42 | # private function |
| 43 | # call at the beginning of each procedure |
| 44 | # defines: _keys _values _ptr |
| 45 | # |
| 46 | # Usage: _inihash NAME |
| 47 | local name=$1 |
| 48 | _keys=_${name}_keys |
| 49 | _values=_${name}_values |
| 50 | _ptr=_${name}_ptr |
| 51 | } |
| 52 | |
| 53 | function newhash () { |
| 54 | # Usage: newhash NAME |
| 55 | # NAME should not contain spaces or dots. |
| 56 | # Actually: it must be a legal name for a Bash variable. |
| 57 | # We rely on Bash automatically recognising arrays. |
| 58 | local name=$1 |
| 59 | local _keys _values _ptr |
| 60 | _inihash ${name} |
| 61 | eval ${_ptr}=0 |
| 62 | } |
| 63 | |
| 64 | |
| 65 | function addhash () { |
| 66 | # Usage: addhash NAME KEY 'VALUE with spaces' |
| 67 | # arguments with spaces need to be quoted with single quotes '' |
| 68 | local name=$1 k="$2" v="$3" |
| 69 | local _keys _values _ptr |
| 70 | _inihash ${name} |
| 71 | |
| 72 | #echo "DEBUG(addhash): ${_ptr}=${!_ptr}" |
| 73 | |
| 74 | eval let ${_ptr}=${_ptr}+1 |
| 75 | eval "$_keys[${!_ptr}]=\"${k}\"" |
| 76 | eval "$_values[${!_ptr}]=\"${v}\"" |
| 77 | } |
| 78 | |
| 79 | function gethash () { |
| 80 | # Usage: gethash NAME KEY |
| 81 | # Returns boing |
| 82 | # ERR=0 if entry found, 1 otherwise |
| 83 | # That's not a proper hash -- |
| 84 | #+ we simply linearly search through the keys. |
| 85 | local name=$1 key="$2" |
| 86 | local _keys _values _ptr |
| 87 | local k v i found h |
| 88 | _inihash ${name} |
| 89 | |
| 90 | # _ptr holds the highest index in the hash |
| 91 | found=0 |
| 92 | |
| 93 | for i in $(seq 1 ${!_ptr}); do |
| 94 | h="\${${_keys}[${i}]}" # Safer to do it in two steps, |
| 95 | eval k=${h} #+ especially when quoting for spaces. |
| 96 | if [ "${k}" = "${key}" ]; then found=1; break; fi |
| 97 | done; |
| 98 | |
| 99 | [ ${found} = 0 ] && return 1; |
| 100 | # else: i is the index that matches the key |
| 101 | h="\${${_values}[${i}]}" |
| 102 | eval echo "${h}" |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | function keyshash () { |
| 107 | # Usage: keyshash NAME |
| 108 | # Returns list of all keys defined for hash name. |
| 109 | local name=$1 key="$2" |
| 110 | local _keys _values _ptr |
| 111 | local k i h |
| 112 | _inihash ${name} |
| 113 | |
| 114 | # _ptr holds the highest index in the hash |
| 115 | for i in $(seq 1 ${!_ptr}); do |
| 116 | h="\${${_keys}[${i}]}" # Safer to do it in two steps, |
| 117 | eval k=${h} #+ especially when quoting for spaces. |
| 118 | echo -n "'${k}' " |
| 119 | done; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | # ----------------------------------------------------------------------- |
| 124 | |
| 125 | # Now, let's test it. |
| 126 | # (Per comments at the beginning of the script.) |
| 127 | newhash Lovers |
| 128 | addhash Lovers Tristan Isolde |
| 129 | addhash Lovers 'Romeo Montague' 'Juliet Capulet' |
| 130 | |
| 131 | # Output results. |
| 132 | echo |
| 133 | gethash Lovers Tristan # Isolde |
| 134 | echo |
| 135 | keyshash Lovers # 'Tristan' 'Romeo Montague' |
| 136 | echo; echo |
| 137 | |
| 138 | |
| 139 | exit 0 |
| 140 | |
| 141 | # Exercise: |
| 142 | # -------- |
| 143 | |
| 144 | # Add error checks to the functions. |