在bash 3创建关联数组 [英] Create associative array in bash 3

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

问题描述

后一种方式来创建Bash关联数组彻底搜索,我发现声明-A阵列会做的伎俩。但问题是,它只是对于bash版本4和bash的版本的服务器在我们的系统是3.2.16。

After thoroughly searching for a way to create an associative array in bash, I found that declare -A array will do the trick. But the problem is, it is only for bash version 4 and the bash version the server has in our system is 3.2.16.

我如何能实现在bash 3某种关联数组般的黑客?该值将被传递给脚本像

How can I achieve some sort of associative array-like hack in bash 3? The values will be passed to a script like

ARG=array[key];

./script.sh ${ARG}

编辑:我知道我可以在AWK做到这一点,或其他工具,但严格的bash是需要我试图解决方案

I know that I can do this in awk, or other tools but strict bash is needed for the scenario I am trying to solve.

推荐答案

3猛砸没有关联数组,所以你将不得不使用一些其他的语言特性(S)为你的目的。请注意,即使在bash 4,你写的code没有做你要求它做什么: ./ script.sh $ {ARG} 未通过关联数组孩子剧本,因为 $ {ARG} 扩展到什么时候 ARG 是一个关联数组。你不能传递一个关联数组一个子进程,则需要连接code也无妨。

Bash 3 has no associative arrays, so you're going to have to use some other language feature(s) for your purpose. Note that even under bash 4, the code you wrote doesn't do what you claim it does: ./script.sh ${ARG} does not pass the associative array to the child script, because ${ARG} expands to nothing when ARG is an associative array. You cannot pass an associative array to a child process, you need to encode it anyway.

您需要定义父脚本和子脚本之间的一些参数传递协议。一个常见的​​一种是传递参数的形式的key = value 。这假定字符 = 没有出现在密钥。

You need to define some argument passing protocol between the parent script and the child script. A common one is to pass arguments in the form key=value. This assumes that the character = does not appear in keys.

您还需要弄清楚如何重新present父脚本和子脚本中的关联数组。他们不必使用相同的重新presentation。

You also need to figure out how to represent the associative array in the parent script and in the child script. They need not use the same representation.

一个常用的方法来重新present关联数组是使用单独的变量的每个元素,具有共同的命名preFIX。这要求键名仅由ASCII字符(这两种情况下的),数字和下划线。例如,而不是 $ {myArray的[关键]} ,写 $ {myarray__key} 。如果密钥在运行时确定,需要一轮扩张第一:代替 $ {myArray的[$关键]} ,写

A common method to represent an associative array is to use separate variables for each element, with a common naming prefix. This requires that the key name only consists of ASCII letters (of either case), digits and underscores. For example, instead of ${myarray[key]}, write ${myarray__key}. If the key is determined at run time, you need a round of expansion first: instead of ${myarray[$key]}, write

n=myarray__${key}; echo ${!n}

有关的分配,使用的printf -v 。注意%S 格式的printf 使用指定的值。不要写 的printf -vmyArray的__ $ {键}%S$值 ,因为这将治疗 $值作为格式,并对其执行的printf 扩张。

For an assignment, use printf -v. Note the %s format to printf to use the specified value. Do not write printf -v "myarray__${key}" %s "$value" since that would treat $value as a format and perform printf % expansion on it.

printf -v "myarray__${key}" %s "$value"

如果你需要传递一个关联数组重新psented像这样子进程与的key = value 参数重新presentation,你可以使用$ P $ $ {!myArray的__ *} 来枚举名称以 myArray的__ 所有的变量。

If you need to pass an associative array represented like this to a child process with the key=value argument representation, you can use ${!myarray__*} to enumerate over all the variables whose name begins with myarray__.

args=()
for k in ${!myarray__*}; do
  n=$k
  args+=("$k=${!n}")
done

在子过程中,参数转换形式的key = value 来了preFIX独立的变量:

In the child process, to convert arguments of the form key=value to separate variables with a prefix:

for x; do
  if [[ $x != *=* ]]; then echo 1>&2 "KEY=VALUE expected, but got $x"; exit 120; fi
  printf -v "myarray__${x%%=*}" %s "${x#*=}"
done

顺便说一句,你确定这是你需要什么?而不是调用从另一个bash脚本bash脚本,你可能要在子shell中运行孩子脚本,而不是。这样,它就会从母公司的所有变量继承。

By the way, are you sure that this is what you need? Instead of calling a bash script from another bash script, you might want to run the child script in a subshell instead. That way it would inherit from all the variables of the parent.

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

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