Bash script (bashmap): a function to complement bash without hashmap __ function

Source: Internet
Author: User
Tags md5

@2013/7/19

-Add the bashmap_clear command to delete the key or empty the entire map. Step 1: Add the function to the bash script (or. bashrc)

# for Bashmap {# echo MD5 code for $ MD5 () {if [x "$ = = X"] then echo "" Else echo "| Md5sum-|
Cut-c 1-32 fi} # Usage # > Bashmap "Key" "Value" # set Map[key] = value.

  # > Bashmap "key" # Print Map[key] Bashmap () {wx_bashmap_prefix= "BASHMAP_" Export Wx_bashmap_prefix
  md5key=$ (eval "MD5") case "$#" in 1) eval "echo \$ $WX _bashmap_prefix$md5key";;
  2) eval "Export $WX _bashmap_prefix$md5key= ' $ '";;
  * echo ' Usage: ' echo ' bashmap ' key ' ' value ' ' echo ' bashmap ' key '; ESAC} # Usage: #    > bashmap_clear      # Clear all map items #    > Bashmap_c Lear key  # clear a map time by Key Bashmap_clear () {  wx_bashmap_prefix= "BASHMAP_"   export WX_BASHMAP_PR Efix   Case "$#" in   0)     to Env in $ (set | grep "^ $WX _bashmap_prefix" | cut-d ' = '-f1)   & nbsp Do       unset $env   &nbsP
Done    ;;
  1)     md5key=$ (eval "MD5 '")     unset $WX _bashmap_prefix$md5key    ;;
   ESAC} # for Bashmap}


Step 2: Use Bashmap to set Key:value

> Bashmap a ' value for A '

> Bashmap b ' value for B '

> Bashmap "a B" "value for a B"



Step 3: The utility Bashmap obtains the key corresponding value, if does not have the default key then returns the empty string ""

> Bashmap a
value for a

> Bashmap b
value for b

> bashmap "A B"
value for a B


Step 4: Delete the specified key

> bashmap_clear "B"

Step 5: Empty the map

> Bashmap_clear


Have fun! :)




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.