簡體   English   中英

如何在 bash 的 for 循環內迭代 2 個 arrays

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

我正在編寫一個腳本,該腳本應根據環境的使用類型和發布日期在環境中安裝修補程序。 為此,我需要檢查環境類型以及修補程序的發布日期。 如果環境的使用類型是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

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