我正在使用 2 个不同的配置文件,我想在我的脚本中加载它们。
目前我可以访问在一个文件(file.config)中声明的变量,但不能访问在另一个文件(file1.config)中声明的关联数组。如何配置以便我也可以访问其他文件。
file.config & file1.config 与脚本位于同一目录中
脚本.sh
#Base Directory
base_dir=`pwd`
Funct () {
. $base_dir/file.config
}
Funct1 () {
. $base_dir/file1.config
}
#Calling function to load config file(file.config)
Funct
echo $var1
#Calling function to load config file(file1.config)
Funct1
echo ${assArray1[flower]}
文件配置var1=val1
var2=val2
文件 1.config declare -A assArray1
assArray1[fruit]=Mango
assArray1[flower]=Rose
输出 : val1
问题:无法打印关联数组元素 - echo ${assArray1[flower]}
最佳答案
declare
在函数内调用 (Funct1
) 使相应的变量成为该函数的局部变量。所以外面是无法访问的。
您可以简单地省略 declare
在 file1.config
– 或使用 declare -g
根据 dan 的建议声明一个全局变量。
关于linux - 无法从配置文件访问关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70923695/