Is there a way to create key-value pairs in Bash script?

后端 未结 5 2094
不思量自难忘°
不思量自难忘° 2020-12-22 19:05

I am trying to create a dictionary of key value pair using Bash script. I am trying using this logic:

declare -d dictionary
defaults write \"$dictionary\" ke         


        
5条回答
  •  臣服心动
    2020-12-22 19:30

    If you can use a simple delimiter, a very simple oneliner is this:

    for i in a,b c_s,d ; do 
      KEY=${i%,*};
      VAL=${i#*,};
      echo $KEY" XX "$VAL;
    done
    

    Hereby i is filled with character sequences like "a,b" and "c_s,d". each separated by spaces. After the do we use parameter substitution to extract the part before the comma , and the part after it.

提交回复
热议问题