簡體   English   中英

是否可以使用bash在for循環中訪問多個數組

[英]is it possible to use bash to access more than one array in a for loop

我正在嘗試編寫一個bash腳本,該腳本可讓我使用curl下載多個網頁。 對於每個網頁,我希望能夠傳遞卷曲頁面和引薦鏈接。 我希望能夠一次提供多個網頁。

換句話說,我希望能夠遍歷我提供的腳本的網頁,並為每個頁面傳遞關聯的網頁和引薦鏈接以進行卷曲。

我以為可以使用數組將網頁和引薦鏈接存儲在單個變量中,以為我可以在運行curl時提取數組的各個元素。

我的問題是我無法弄清楚如何使多個數組在for循環中正常工作。 這是我要做什么的想法。 此代碼不起作用,因為“ $ i”(在for循環中)不會成為數組。

#every array has the information for a separate webpage
array=( "webpage" "referer" )
array2=( "another webpage" "another referer" )

for i in "${array[@]}" "${array2[@]}" #line up multiple web pages
do
    #use curl to download the page, giving the referer ("-e")
    curl -O -e "${i[1]}" "${i[0]}"
done

如果我只使用一個數組,則可以很容易地做到這一點:

array=( "webpage" "referer" )
REFERER="${array[1]}"
PAGE="${array[0]}"
#use curl to download the page, giving the referer ("-e")
curl -O -e "$REFERER" "$LINK"

一旦我要一次處理多個網頁,就無法弄清楚如何正確地進行處理。

如果還有另一種處理多個網頁的方法,而不必使用數組和for循環,請告訴我。

如果還有另一種處理多個網頁的方法,而不必使用數組和for循環,請告訴我。

使用數組很好,至少比使用空格分隔的列表或類似的技巧好得多。 只需循環索引:

array=('webpage' 'another webpage')
array2=('referrer' 'another referrer')
# note the different layout!
for i in "${!array[@]}"
do 
    webpage="${array[$i]}"
    referrer="${array2[$i]}"
done

您需要一個技巧。 請注意,URL中不允許使用空格,因此您可以說:

webpages=("url referrer" "url2 ref2" ...)

for i in "${webpages[@]}" ; do
    set -- "$i"
    url="$1"
    ref="$2"

    curl -O -e "${url}" "${ref}"
done

[編輯]也許更好的解決方案是將所有URL放入文件中,然后使用以下代碼:

while read url ref ; do
    curl -O -e "${url}" "${ref}"
done < file

或者,如果您喜歡這里文件

while read url ref ; do
   echo "url=$url ref=$ref"
done <<EOF
url1 ref1
url2 ref2
... xxx
EOF

感謝大家的回應。 兩種想法都有優點,但是我在《 高級Bash指南》中找到了一些完全符合我想要做的代碼。

我不能說我完全理解它,但是通過使用對數組的間接引用,我可以在for循環中使用多個數組。 我不確定本地命令是做什么的,但這是關鍵(我認為它運行某種eval並將字符串分配給變量)。

這樣做的好處是,我可以將每個網頁和引薦來源分組到各自的數組中。 然后,我可以通過創建一個新數組並將其添加到for循環中來輕松添加一個新網站。 另外,如果我需要向curl命令添加更多變量(例如cookie),則可以輕松擴展數組。

function get_page () {
        OLD_IFS="$IFS"
        IFS=$'\n'       #  If the element has spaces, when using
                        #  local to assign variables

        local ${!1}


        # Print variable
        echo First Variable: "\"$a\""
        echo Second Variable: "\"$b\""
        echo ---------------
        echo curl -O -e "\"$a\"" "\"$b\""
        echo  
        IFS="$OLD_IFS"
}       

#notice the addition of "a=" and "b="
#this is not an associative array, that would be [a]= and [b]=
array=( a="webpage" b="referer" )
array2=( a="another webpage" b="another referer" )

#This is just a regular string in the for loop, it doesn't mean anything
#until the indirect referencing later
for i in "array[*]" "array2[*]" #line up multiple web pages
do
        #must use a function so that the local command works
        #but I'm sure there's a way to do the same thing without using local
        get_page "$i" 
done

結果是:

First Variable: "webpage"
Second Variable: "referer"
---------------
curl -O -e "webpage" "referer"

First Variable: "another webpage"
Second Variable: "another referer"
---------------
curl -O -e "another webpage" "another referer"

就像一般情況一樣:在函數內至少應聲明IFS變量以將其范圍限制為僅對該函數。 無需通過OLD_IFS保存和還原IFS!

help declare

IFS=$' \t\n'
printf "%q\n" "$IFS"

function ifs_test () {
    declare IFS
    IFS=$'\n'
    printf "%q\n" "$IFS"
    return 0
}

ifs_test

printf "%q\n" "$IFS"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM