home *** CD-ROM | disk | FTP | other *** search
/ chilidog.highland.cc.ks.us / chilidog.highland.cc.ks.us.zip / chilidog.highland.cc.ks.us / backup / bradford.20110725.etc.tar.gz / bradford.20110725.etc.tar / etc / sysconfig / scripts / SuSEfirewall2-bashhash < prev    next >
Text File  |  2006-04-22  |  2KB  |  97 lines

  1. #!/bin/bash
  2. # SuSEfirewall2-bashhash - hash emulation in bash
  3. # Copyright (C) 2004 SUSE LINUX Products GmbH
  4. #
  5. # Author:     Ludwig Nussel
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # version 2 as published by the Free Software Foundation.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  16.  
  17. # isn't that sick?
  18.  
  19. hashlookup()
  20. {
  21.     local h hmatch key q
  22.     if [ "$1" = "-q" ]; then
  23.     q=1
  24.     shift
  25.     fi
  26.     h="hash_$1"
  27.     key="$2"
  28.     eval hmatch="\$hash_match_$h"
  29.  
  30.     case "$key" in
  31.     *\ *|*-*|*/*) return 1 ;;
  32.     esac
  33.  
  34.     eval "case $key in
  35.     $hmatch)
  36.         [ -z "$q" ] && eval echo '\$hash_key_${h}_$key'
  37.         return 0
  38.     ;;
  39.     esac"
  40.     return 1
  41. }
  42.  
  43. hashadd()
  44. {
  45.     local h hmatch key val
  46.     h="hash_$1"
  47.     key="$2"
  48.     val="$3"
  49.     eval hmatch="\$hash_match_$h"
  50.     case $key in
  51.     $hmatch) ;;
  52.     *)
  53.         if [ -z "$hmatch" ]; then
  54.         eval hash_match_$h="\"\$key\""
  55.         else
  56.         eval hash_match_$h="\"\$hmatch|\$key\""
  57.         fi
  58.     ;;
  59.     esac
  60.  
  61.     eval hash_key_${h}_$key="\$val"
  62. }
  63.  
  64. hashallkeys()
  65. {
  66.     local h hmatch i
  67.     h="hash_$1"
  68.     eval hmatch="\$hash_match_$h"
  69.     IFS="|" eval echo "\$hmatch"
  70. }
  71.  
  72. if [ "$1" = "test" ]; then
  73.     hashadd h "foo" "bar"
  74.     hashadd h "bla" "blub"
  75.     hashadd h "red" "blue"
  76.     hashadd h "green" "yellow"
  77.  
  78.     for i in ${!hash*}; do
  79.     eval echo \"$i=\$$i\"
  80.     done
  81.  
  82.     hashlookup h foo || echo foo not in hash
  83.     hashlookup h blue || echo blue not in hash
  84.     hashlookup h green || echo green not in hash
  85.     hashlookup -q h green || echo green not in hash
  86.     hashlookup h "x y" || echo "\"x y\" invalid"
  87.     hashlookup h x-y || echo "x-y invalid"
  88.  
  89.     echo -n "all keys: "
  90.     hashallkeys h
  91. fi
  92.  
  93. # vim:sw=4
  94.