有没有一种方法来创建Bash脚本键 - 值对? [英] Is there a way to create key-value pairs in Bash script?
本文介绍了有没有一种方法来创建Bash脚本键 - 值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个使用Bash脚本键值对的字典。我使用这个逻辑尝试:
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" key -string "$value"
...其中 $字典
是一个变量,但是这是行不通的。
...where $dictionary
is a variable, but this is not working.
有没有一种方法来创建Bash脚本键 - 值对?
Is there a way to create key-value pairs in Bash script?
推荐答案
在bash的版本4的关联数组进行了介绍。
In bash version 4 associative arrays were introduced.
declare -A arr
arr["key1"]=val1
arr+=( ["key2"]=val2 ["key3"]=val3 )
的ARR数组现在包含三个键值对。 Bash是相当有限,你可以与他们虽然做什么,不排序或爆裂等。
The arr array now contains the three key value pairs. Bash is fairly limited what you can do with them though, no sorting or popping etc.
for key in ${!arr[@]}; do
echo ${key} ${arr[${key}]}
done
将循环在所有键值和回声出来。
Will loop over all key values and echo them out.
这篇关于有没有一种方法来创建Bash脚本键 - 值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文