首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在bash中的for循环中迭代2个数组

问如何在bash中的for循环中迭代2个数组
EN

Stack Overflow用户
提问于 2020-04-27 16:31:09
回答 1查看 39关注 0票数 0

我正在编写一个脚本,它应该根据环境的使用类型和发布日期在环境上安装修补程序。为此,我需要检查环境类型,然后检查修补程序的发布日期。如果环境的使用类型为prod,并且自修补程序发布以来已经过time1秒数,则安装修补程序。

通过阅读网站上类似的问题,我想到了这一点。

代码语言:javascript
复制
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

上面的代码会按预期工作吗?

编辑:

我试着修正了语法,但还是遗漏了一些东西:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-27 19:02:41

如果你的问题是“如何迭代两个数组”,你可以通过运行下面的代码片段来检查这部分代码是否正确:

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
u[0] = prod, t[0] = time2
u[1] = test, t[1] = time2
u[2] = dev, t[2] = time3
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61454691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档