简体   繁体   English

如何在 bash 的 for 循环内迭代 2 个 arrays

[英]How to iterate over 2 arrays inside a for loop in bash

I am working on a script that should install hotfixes on environments depending on the usage type of the environment and the release date.我正在编写一个脚本,该脚本应根据环境的使用类型和发布日期在环境中安装修补程序。 For this, I need to check the type of environment and then the release date of the hotfix.为此,我需要检查环境类型以及修补程序的发布日期。 If the environment's usage type is prod and time1 number of seconds have passed since the release of the hotfix, then install the hotfix.如果环境的使用类型是prod并且time1自发布修补程序以来已经过去了秒数,则安装修补程序。

By reading similar questions on the site, I came up with this.通过阅读网站上的类似问题,我想出了这个。

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

Would the above code work as intended?上面的代码会按预期工作吗?

EDIT:编辑:

I tried fixing the syntax, but I am still missing something:我尝试修复语法,但我仍然缺少一些东西:

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

If your question is "how to iterate over two arrays", you can check that that part of your code is correct by running next snippet:如果您的问题是“如何遍历两个数组”,您可以通过运行下一个片段来检查您的代码部分是否正确:

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

Output: Output:

u[0] = prod, t[0] = time2
u[1] = test, t[1] = time2
u[2] = dev, t[2] = time3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM