是否有可能使用bash访问多个阵列中的for循环 [英] is it possible to use bash to access more than one array in a for loop

查看:174
本文介绍了是否有可能使用bash访问多个阵列中的for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个bash脚本,可以让我下载使用curl多个网页。对于每一个网页,我希望能够通过卷曲的页面以及引荐链接。我希望能够同时提供多个网页。

I'm trying to write a bash script that will let me download multiple web pages using curl. For each webpage, I want to be able to pass curl the page and the referer link. I want to be able to supply multiple webpages at once.

在换句话说,我希望能够遍历我提供脚本的网页,并为每个网页,通过网页相关和referer链接卷曲。

In other words, I want to be able to loop through the webpages I supply the script, and for each page, pass the associated webpage and referer link to curl.

我想我会用一个数组来存储在一个单一变量的网页,并引用者联系,以为运行卷曲的时候才能然后提取该数组的单个元素。

I thought I'd use an array to store the webpage and referer link in a single variable, thinking that I could then extract the individual elements of the array when running curl.

我的问题是,我无法弄清楚如何让多个阵列在一个正常的循环。这里是什么我想要做的想法。这code不起作用,因为$ I(在for循环)不会成为一个数组。

My problem is that I can't figure out how to get multiple arrays to work properly in a for loop. Here is an idea of what I want to do. This code does not work, since "$i" (in the for loop) doesn't become an array.

#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

如果我只用一个阵列的工作,我可以很容易地做到这一点是这样的:

If I was only working with one array, I could easily do it like this:

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

这一次,我有我想一次,我无法弄清楚如何做是正确的处理多个网页。

It's once I have more than one webpage that I want to process at once that I can't figure out how to do it correctly.

如果有另一种方式来处理多个网页,而不必使用数组和一个for循环,请让我知道。

If there is another way to handle multiple webpages, without having to use arrays and a for loop, please let me know.

推荐答案

感谢大家对他们的答复。这两种观点有可取之处,但我发现了一些code中的高级Bash指南这不正是我想做的事情。

Thanks to everyone for their responses. Both ideas had merit, but I found some code in the Advanced Bash Guide that does exactly what I want to do.

我不能说我完全理解,但是通过使用间接引用数组,我可以在使用多个阵列for循环。我不知道当地的命令做什么,但它是关键(我认为它运行一种评估和字符串赋值给变量)。

I can't say I fully understand it, but by using an indirect reference to the array, I can use multiple arrays in the for loop. I'm not sure what the local command does, but it is the key (I think it runs a sort of eval and assigns the string to the variable).

这样做的好处是,我可以组每个网页,并引荐到各自为阵。然后,我可以轻松地添加一个新的网站,通过创建一个新的数组并将其添加到for循环。另外,我应该需要增加更多的变数curl命令(如饼干),我可以很容易地扩展磁盘阵列。

The advantage of this is that I can group each webpage and referer into their own array. I can then easily add a new website, by creating a new array and adding it to the for loop. Also, should I need to add more variables to the curl command (such as a cookie), I can easily expand the array.

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"

这篇关于是否有可能使用bash访问多个阵列中的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆