arrays - 如何在bash的for循环中迭代2个数组
问题描述
我正在编写一个脚本,该脚本应根据环境的使用类型和发布日期在环境中安装修补程序。为此,我需要检查环境类型以及修补程序的发布日期。如果环境的使用类型是prod
和time1
自发布修补程序以来经过的秒数,则安装修补程序。
通过阅读网站上的类似问题,我想出了这个。
u=( prod test dev)
t=( time2 time2 time3 )
# where t represents the number of seconds that must pass after the release date in order for the hotfix to be installed
for ((i=0;i<${#u[@]};i++))
do
if ($usage_type=${u[i]} && $hf_release_date -ge $current_time+${t[i]}); then install_hotfix; fi
done
上面的代码会按预期工作吗?
编辑:
我尝试修复语法,但我仍然缺少一些东西:
u=( prod test dev)
t=( time2 time2 time3 )
# where t represents the number of seconds that must pass after the release date in order for the hotfix to be installed
for ((i=0;i<${#u[@]};i++))
do
if [[ "$usage_type" == "${u[i]}" ]] && [[ "$hf_release_date" -ge "$current_time"+"${t[i]}" ]]; then install_hotfix; fi
done
解决方案
如果您的问题是“如何遍历两个数组”,您可以通过运行下一个片段来检查您的代码部分是否正确:
u=( prod test dev)
t=( time2 time2 time3 )
for ((i=0;i<${#u[@]};i++))
do echo "u[$i] = ${u[i]}, t[$i] = ${t[i]}"
done
输出:
u[0] = prod, t[0] = time2
u[1] = test, t[1] = time2
u[2] = dev, t[2] = time3
推荐阅读
- sql-server - 子查询返回超过 1 个值。当子查询跟随 !=、<、<=、>、>= 时,这是不允许的
- python - 在 Python 中使用全局变量和队列类进行线程数据通信有什么区别?
- docker - Kubernetes 执行程序 pod 创建失败,出现间歇性 404 和“未经授权”错误
- c++ - 是否有特殊原因将小于运算符用作 max pq 的默认比较?
- php - 用于从字段中删除所有标签的 MY SQL 查询
- flask - flask-migrate:如何添加新数据库
- dynamic-programming - 动态规划。硬币行问题 - 如何发展其递归关系
- cassandra - Cassandra:不正确的复制命令
- reactjs - reactjs中firestore的新文档参考
- javascript - 一次在 Rx.Observable 中执行的一个 Promise