简体   繁体   中英

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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