【问题标题】:unable to pass wget a variable with quotes inside the variable无法在变量内传递带引号的 wget 变量
【发布时间】:2013-04-30 02:54:14
【问题描述】:

我正在尝试编写一个 wget 命令来下载网页及其所有附件和 jpeg 等。

当我手动输入脚本时,它可以工作,但我需要运行超过 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.

如您所见,Host 和 Cookie 部分的格式不正确,导致 wget 命令无法登录并提取数据。

我一直在阅读 bash 手册页、谷歌搜索,并尝试了一些来自 SO 的相关建议,但我仍然无法执行命令。

有人会很好地告诉我在veriables中引用引号的正确方法吗?

谢谢,

【问题讨论】:

    标签: linux bash variables wget quotes


    【解决方案1】:

    带引号的字符串或变量中的引号是普通字符,而不是引号字符。没有办法改变它。改用数组:

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

    使用四个参数abc de f 调用cmd

    (您可以使用eval 实现类似的效果,但这更容易出错。在您的情况下,使用数组更方便。)

    【讨论】:

    • Uwe,很抱歉对此感到愚蠢,但我看不出这会有什么不同,因为我的字符串包含引号,即 "--header=\'Host: mywebsite.com: 9090\' 您能否详细说明我将如何在数组中输入变量值以便 Bash 正确读取引号?
    • 或者我使用的格式是否正确,并且通过使用数组,Bash 将开始正确解释它们?
    • 定义session=(--header='Host: mywebsite.com:9090' --header='User-Agent: ... Firefox/20.0'),然后使用"${session[@]}"而不是$session。其他变量必须进行类似的修改。
    • 谢谢,完美。现在我需要去研究数组是如何工作的,这样我才能真正理解它在做什么。非常感谢您的答案和示例。
    【解决方案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"
    

    用这个,

    【讨论】:

    • 如果你这样做,那么 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
    • 上面的评论是正确的。这正是我在提出关于 SO 的问题之前尝试此操作时在输出中看到的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多