庆典:数组设置环境变量和取消引用它从任何shell脚本失败 [英] bash: set array env variable and de-referencing it from any shell script fails

查看:213
本文介绍了庆典:数组设置环境变量和取消引用它从任何shell脚本失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了​​一个数组作为一个环境变量以这种方式
 例如。脚本 test.sh

I set the a array as an environment variable in this manner eg. script test.sh

在test.sh

#!/bin/bash
export STRING=( "str1" "str2" )

源test.sh

source test.sh

现在的剧本 test-1.sh

#!/bin/bash
echo ${STRING[0]}

的反应是什么,只是一个空行,
  而如果我尝试设置 = STRINGSTR1在test.sh
  做回声$ STRING test-1.sh ,这个工程。

the response is nothing, just a blank line, whereas, if I try to set STRING="str1" in test.sh and do echo $STRING in test-1.sh, this works.

测试是从root用户才能执行,
  现在,如何设置数组作为环境变量,这样我可以调用按规定数组的元素?此前,我曾尝试甚至修改的/ etc / bashrc中键,也没有造成任何积极。

tests are executed from root user only, Now how to set array as env variable , so that I can call the elements of array as per requirement? Earlier, I have tried to even modify /etc/bashrc and that also didn't result in anything positive.

我需要设置数组作为环境变量,因为可能有很多的脚本,我必须写这应使用这些变量的设置。

I need to set the array as env variable as there may be many scripts that i have to write which shall use these variable settings.

有谁能够给我提供建议,以纠正我哪里做错了吗?

can anybody provide me suggestions to correct me where I am doing wrong?

推荐答案

阅读精细的手工,错误一节。

Read the fine manual, "bugs" section.

数组变量可能(还)没有出口。

Array variables may not (yet) be exported.

虽然,我不知道,许多人认为这确实是个bug。支持KSH风格的数组其他炮弹不允许要么把它们导出。

Though, I don't know that many consider this an actual bug. Other shells that support ksh-style arrays don't allow exporting them either.

您可以绕过数组的定义比较容易,通过参数或变量或环境。它虽然通常不是非常有用的。

You may pass around array definitions rather easily, through parameters or variables or the environment. It isn't usually very useful though.

function f {
    unset -v "$2"
    typeset "$2"
    eval "${!1}"
    typeset -p "$2"
}

typeset -a a=(a b c)
myArr=$(typeset -p a) f myArr a

这篇关于庆典:数组设置环境变量和取消引用它从任何shell脚本失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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