Created
          August 18, 2015 00:48 
        
      - 
      
- 
        Save bertrandmartel/ff760be46960efc281dc to your computer and use it in GitHub Desktop. 
    [ LINUX BASH ] Associate map : get / set / insert
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/bin/bash | |
| declare -A map=([toto]=1 [tata]=2) | |
| YOUR_STRING="titi" | |
| CHECK=`echo "${!map[@]}" | grep -o $YOUR_STRING` | |
| if [ -z $CHECK ]; then | |
| #create the entry for it and set its count (value) to 1 | |
| map[$YOUR_STRING]=0 | |
| else | |
| # increment the count (value) by 1 | |
| var="${map[$YOUR_STRING]}" | |
| map[$YOUR_STRING]=$((var+1)) | |
| fi | |
| echo "your key : $YOUR_STRING | your value : ${map[$YOUR_STRING]}" | |
| # A quick solution => (( ++map["$YOUR_STRING"] )) | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment