arrays - 数组内的bash变量扩展
问题描述
如何从数组列表中的变量传递值。
下面的代码有效,它将 repo_libs 元素分配给 repo_list。有没有办法从变量中读取值以在数组列表中形成数组名称。
if [[ "$service" == "all" || "$service" == "libs" ]] ; then
# below working
repo_list=("${repo_libs[@]}")
echo "repo_list : ${repo_list[*]}"
fi
寻找如下陈述
repo_list=(${repo_`echo $service`[@]})
repo_list=("${repo_$service[@]}")
看到如下错误。
./build.sh: line 168: ${repo_`echo $service`[@]}: bad substitution
./build.sh: line 173: ${repo_$service[@]}: bad substitution
[编辑] 这是使用 if .. else 作为我正在使用的替代方法的代码。
if [ "$service" = "all" ] ; then
repo_list=("${repo_all[@]}")
elif [ "$service" = "libs" ] ; then
repo_list=("${repo_libs[@]}")
elif [ "$service" = "portlets" ] ; then
repo_list=("${repo_portlets[@]}")
else
repo_list=("$service")
fi
解决方案
推荐阅读
- java - Spring会话在会话超时后创建新会话而不进行身份验证
- python - 用于字符串表达式的递归括号解析器
- sql - 使用 Convert() 将 nvarchar 值“3.5”转换为数据类型 int 时转换失败
- angular - Angular Mat-table 扩展和折叠未按预期工作
- c# - 如何将 Table2 列添加到 table1
- c# - 比较两个文本相同的字符串,只替换字符串后面
- python - 通过 Pyqt5 python 保存文件
- r - R - 使用 purrr::pmap() 进行逐行迭代
- php - 数据库抽象层——那是什么?
- r - 关于特定条件下 group_by 的总和