如何使用setenv [重复]为bash数组设置值

[英]How to set value for bash array using setenv [duplicate]


This question already has an answer here:

这个问题在这里已有答案:

I wrote a C program in Linux to set the values of environment variables using setenv, but I cannot set value for array variable (I printed the value of the array to an output file, but the content of the file is blank), but non-array variables are working fine. Here is the code snippet:

我在Linux中编写了一个C程序来使用setenv设置环境变量的值,但是我不能为数组变量设置值(我将数组的值打印到输出文件,但文件的内容是空白的),但是非-array变量工作正常。这是代码片段:

setenv("header", "Welcome: ", 1);             // work fine, can output to file
setenv("info[0]", "192.168.1.1: ", 1);        // nothing are shown in output file
setenv("info[1]", "AA-AA-AA-BB-BB-BB: ", 1);  // nothing are shown in output file

I cannot find any reason why it not work :( Any help is appreciated.

我找不到任何理由为什么它不起作用:(任何帮助表示赞赏。

1 个解决方案

#1


1  

Arrays are handled by bash, they don't exist as environment variables. Environment variables are stored in kernel per process as VARNAME=value, you can check this:

数组由bash处理,它们不作为环境变量存在。环境变量作为VARNAME = value存储在每个进程的内核中,您可以检查:

$ cat /proc/$$/environ | tr '\0' '\n'

But you can use array in the form of string with delimiter (coma or tab sign or whatever):

但是您可以使用带分隔符的字符串形式的数组(逗号或制表符号或其他):

info=192.168.1.1:,AA-AA-AA-BB-BB-BB:

信息= 192.168.1.1:,AA-AA-AA-BB-BB-BB:

and handle this string in appropriate way

并以适当的方式处理此字符串

Also you should be careful with its names. First of all there is specific requirements on how the name should be composed:

你也应该小心它的名字。首先,对如何组成名称有特定要求:

Environment variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit. Other characters may be permitted by an implementation; applications shall tolerate the presence of such names.

IEEE Std 1003.1-2001的Shell和Utilities卷中的实用程序使用的环境变量名称仅由大写字母,数字和便携式字符集中定义的字符的“_”(下划线)组成,并且不以数字开头。实现可以允许其他字符;申请应容忍此类名称的存在。

The second thing to note is that you risk to modify some variable some other component of program is relying on.

第二点需要注意的是,您可能会修改程序依赖的某些其他组件变量。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/10/22/9573a8543fb1006207ad974476d32673.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号