2013-05-06 55 views
0

我试图脚本wget命令来下载一个网页,它的所有内部附件和JPEG等无法通过wget的一个变量引用变量

当我进入手动脚本,它的工作原理,但我需要运行这35000多次来存档一个不受我控制的旧网站(国际公司政治,但我是数据的所有者)。

我的问题一直在变化会话参数。

我的剧本至今如下:

cnt=35209 
# initialise the headers 
general_settings='-4 -P xyz --restrict-file-names=windows -nc --limit-rate=250k' 
html_page_specific='--convert-links --html-extension' 
proxy='--proxy-user=xxxxxx --proxy-password=yyyyyyy' 
session="--header=\'Host: mywebsite.com:9090\' --header=\'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0\'" 
address=http://mywebsite.com:9090/browse/item-$cnt 

echo $general_settings $proxy $session $cookie $address 
echo 
echo 
echo Getting item-$cnt... 

#while [ $cnt -gt 0 ] 
#do 
# # get the page 
    wget --debug $general_settings $html_page_specific $proxy $session $cookie $address 

    # now get the attachments, pdf, txt, jpg, gif, sql, etc... 
# wget -A.pdf $general_settings -r $proxy $session $cookie $address 
# wget -A.txt $general_settings -r $proxy $session $cookie $address 
# wget -A.jpg $general_settings -r $proxy $session $cookie $address 
# wget -A.gif $general_settings -r $proxy $session $cookie $address 
# wget -A.sql $general_settings -r $proxy $session $cookie $address 
# wget -A.doc $general_settings -r $proxy $session $cookie $address 
# wget -A.docx $general_settings -r $proxy $session $cookie $address 
# wget -A.xls $general_settings -r $proxy $session $cookie $address 
# wget -A.xlsm $general_settings -r $proxy $session $cookie $address 
# wget -A.xlsx $general_settings -r $proxy $session $cookie $address 
# wget -A.xml $general_settings -r $proxy $session $cookie $address 
# wget -A.ppt $general_settings -r $proxy $session $cookie $address 
# wget -A.pptx $general_settings -r $proxy $session $cookie $address 
# wget -A.png $general_settings -r $proxy $session $cookie $address 
# wget -A.ps $general_settings -r $proxy $session $cookie $address 
# wget -A.mdb $general_settings -r $proxy $session $cookie $address 
# ((cnt=cnt-1)) 
# 
#done 

但是当我运行该脚本,我得到下面的输出

Getting item-35209... 
Setting --inet4-only (inet4only) to 1 
Setting --directory-prefix (dirprefix) to xyz 
Setting --restrict-file-names (restrictfilenames) to windows 
Setting --no (noclobber) to 1 
Setting --limit-rate (limitrate) to 250k 
Setting --convert-links (convertlinks) to 1 
Setting --html-extension (htmlextension) to 1 
Setting --proxy-user (proxyuser) to xxxxx 
Setting --proxy-password (proxypassword) to yyyyy 
Setting --header (header) to \'Host: 
Setting --header (header) to 'Cookie: 
DEBUG output created by Wget 1.11.4 Red Hat modified on linux-gnu. 

正如你所看到的,主机和Cookie的部分是不格式正确,导致wget命令无法登录并提取数据。

我一直在阅读bash手册页,使用谷歌搜索,并尝试了几个相关的建议,但我仍然无法获得执行命令。

任何人都会有足够的精神向我展示正确的方式来引用可读性中的引号吗?

感谢,带引号的字符串或变量的内部

回答

4

行情是普通字符,而不是引号字符。没有办法改变这一点。使用数组来代替:

A=(a b 'c d' 'e f') 
cmd "${A[@]}" 

调用cmd有四个参数abc de f

(你可以实现与eval类似的效果,但是这是一个很多更容易出错。在你的情况下,使用阵列是方便多了。)

+0

乌韦, 对不起,这是愚蠢的,但我不明白这将有什么区别,因为我的字符串包含引号,即 “--header = \'主机:mywebsite.com:9090 \' 您能否扩展我将如何在数组中输入变量值,以便可以读取引号Bash是什么意思? – dhevans79 2013-05-06 12:36:44

+0

或者是我使用的格式正确,并且通过使用数组,Bash将开始正确解释它们? – dhevans79 2013-05-06 12:37:37

+0

定义'session =( - header ='Host:mywebsite.com:9090'--header ='User-Agent:... Firefox/20.0')'然后使用'“$ {session [@]}” '而不是'$ session'。其他变量必须进行类似的修改。 – Uwe 2013-05-06 12:42:55

-2
session="--header=Host: mywebsite.com:9090 --header=User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0" 

利用这一点,

+1

如果你这样做,那么'wget'要么用不同的参数'--header = Host:','mywebsite.com:9090','--header = User-Agent:','Mozilla/5.0', (如果使用'$ session'),或者使用一个参数'--header = Host:... Firefox/20.0'(如果使用'$ session“')。这两种方法都没有达到目标,即使用两个参数'--header = Host:mywebsite.com:9090'和'--header = User-Agent:Mozilla/5.0 ... Firefox/20.0'来调用。 – Uwe 2013-05-06 11:45:50

+0

上面的评论是正确的。这正是我在输出时看到的,当我在SO上提问之前尝试了这个。 – dhevans79 2013-05-06 12:39:29