在BASH多维数组 [英] multi-dimensional arrays in BASH

查看:122
本文介绍了在BASH多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算一个脚本来管理Linux系统的一些作品和我在决定点,如果我想使用的庆典或的蟒蛇

I am planning a script to manage some pieces of my Linux systems and am at the point of deciding if I want to use bash or python.

我想preFER做到这一点作为一个Bash脚本仅仅是因为命令是比较容易的,但真正的决定因素是配置。我需要能够存储在配置文件中的多维阵列告诉脚本做什么用本身。存储简单的键=在配置文件值对是使用bash很容易,但我能想到的唯一的方式做一个多维数组是一个两层解析引擎,像

I would prefer to do this as a Bash script simply because the commands are easier, but the real deciding factor is configuration. I need to be able to store a multi-dimensional array in the configuration file to tell the script what to do with itself. Storing simple key=value pairs in config files is easy enough with bash, but the only way I can think of to do a multi-dimensional array is a two layer parsing engine, something like

array=&d1|v1;v2;v3&d2|v1;v2;v3

但马歇尔/解组code能拿是熊和它远离用户友好的下一个可怜虫有管理这一点。如果我不能在bash做到这一点很容易,我会简单地写了CONFIGS一个XML文件,并在Python写剧本。

but the marshall/unmarshall code could get to be a bear and its far from user friendly for the next poor sap that has to administer this. If i can't do this easily in bash i will simply write the configs to an xml file and write the script in python.

有没有一种简单的方法在bash做到这一点?

Is there an easy way to do this in bash?

感谢大家。

推荐答案

击不支持也不多维数组,无论是哈希,似乎你想要一个哈希值是数组。该解决方案是不是很漂亮,有一个xml文件的解决方案应该会更好:

Bash does not support neither multidimensional arrays, neither hashes, and it seems that you want a hash that values are arrays. This solution is not very beautiful, a solution with an xml file should be better :

array=('d1=(v1 v2 v3)' 'd2=(v1 v2 v3)')
for elt in "${array[@]}";do eval $elt;done
echo "d1 ${#d1[@]} ${d1[@]}"
echo "d2 ${#d2[@]} ${d2[@]}"

这篇关于在BASH多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆