linux - 无法从配置文件访问关联

标签 linux bash shell ubuntu

我正在使用 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) 使相应的变量成为该函数的局部变量。所以外面是无法访问的。
您可以简单地省略 declarefile1.config – 或使用 declare -g根据 dan 的建议声明一个全局变量。

关于linux - 无法从配置文件访问关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70923695/

相关文章:

PHP exec 没有按预期运行我的脚本

regex - 使用 Sed 将字符串变量替换为字符串变量

linux - 在 Shell 脚本中使用动态变量生成 JSON

bash - 使用sed在主机文件的行尾添加地址

linux - 如何将参数从 bash 文件传递​​给 awk 文件

c - 全局变量不会在c中的其他线程中改变

linux - 条件变量和rwlock死锁

linux - 我应该在 clone(2) 中设置什么标志,以便它与 pthread_create() 一样工作?

bash - awk 和 sed 拆分文件

java - 从 java 读取时,Linux/bash 环境变量为空