如何将存储在变量中的参数传递给WGET


10

我正在编写一个广泛使用wget的bash脚本。为了在一处定义所有通用参数,我将它们存储在变量中。这是一段代码:

useragent='--user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0"'
cookies_file="/tmp/wget-cookies.txt"
save_cookies_cmd="--save-cookies $cookies_file --keep-session-cookies"
load_cookies_cmd="--load-cookies $cookies_file --keep-session-cookies"

function mywget {
    log "#!!!!!!!!!# WGET #!!!!!!!!!# wget $quiet $useragent $load_cookies_cmd $@"  
    wget $useragent $load_cookies_cmd "$@"
}

遗憾的是没有工作。不知何故,我缺少将参数存储在变量$ useragent,$ save_cookies_cmd,$ load_cookies_cmd以及将这些var作为参数传递的wget上的正确方法。

我想要这样的结果命令行:

wget --user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0" --load-cookies /tmp/wget-cookies.txt --keep-session-cookies http://mysite.local/myfile.php

编辑:我的最终解决方案:

最后,我的脚本可以正确使用:

useragent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0"
useragent_cmd="--user-agent=$useragent"
cookies_file="/tmp/wget-cookies.txt"
save_cookies_cmd="--save-cookies $cookies_file --keep-session-cookies"
load_cookies_cmd="--load-cookies $cookies_file --keep-session-cookies"

function mywget {
    log "#!!!!!!!!!# WGET #!!!!!!!!!# wget $load_cookies_cmd $useragent_cmd $@"  
    wget $load_cookies_cmd "$useragent_cmd" "$@"
}

感谢您的所有回复。

Answers:


4

http://mywiki.wooledge.org/BashFAQ/050

user_agent='--user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0"'
cookies_file="/tmp/wget-cookies.txt"
save_cookies_opts=( --save-cookies "$cookies_file" --keep-session-cookies )
load_cookies_opts=( --load-cookies "$cookies_file" --keep-session-cookies )

function mywget {
    log "#!!!!!!!!!# WGET #!!!!!!!!!# wget $quiet $useragent $load_cookies_cmd $@"
    wget "$user_agent" "${load_cookies_opts[@]}" "$@"
}

请注意,在wget调用中,用"$user_agent"引号引起来。这很关键。


巧妙使用数组。感谢您的答复。它肯定会帮助我理解更好的bash脚本。
Filipe 2014年

3

问题在于扩展的顺序(man bash有关详细信息,请参阅扩展)。扩展变量时,变量内的双引号不能用作双引号:

$ a='1 "2 3" 4' ; for b in $a ; do echo $b ; done
1
"2
3"
4

请注意,使用“ $ b”无济于事。

使用数组,因此您可以让bash进行艰苦的工作,而您可以忘记引号和转义:

$ a=(1 '2 3' 4) ; for b in "${a[@]}" ; do echo $b ; done
1
2 3
4

谢谢你的时间。我是bash脚本的新手,从现在开始,这些技巧将始终为我提供帮助。
Filipe 2014年

3
    mywget() ( echo log "wget $quiet \
                         userstring=${userstring:-unset} \
                         cookies=${cookies:-no} $@"
            echo wget ${userstring:+--useragent="$userstring"} \
                    ${cookies:+--${cookies}-cookies \
                    "$cookies_file" --keep-session-cookies} "$@"
    ) <<-PARAMS
            ${DQ=$(printf \\042)}
            ${NL=
}
            ${userstring="${DQ}Mozilla/5.0 \
                (Windows NT 6.1; WOW64; rv:27.0) \
                Gecko/20100101 Firefox/27.0${DQ}"}
            ${cookies_file="/tmp/wget-cookies.txt"}
            ${cookies=save}
    PARAMS

请原谅我,但我已经剥夺了parameterizing您的职能。因为在值PARAMS设置未设置变量,你可以很容易忽略他们的价值-它允许您设置默认理智,但仍然改变每个函数调用值只要你喜欢。例如,在上面的函数中,我将所有值都设置为echo,这是按原样运行时的输出:

log wget  userstring="Mozilla/5.0 \
    (Windows NT 6.1; WOW64; rv:27.0) \
    Gecko/20100101 Firefox/27.0" \
    cookies=save
wget --useragent="Mozilla/5.0 \
    (Windows NT 6.1; WOW64; rv:27.0) \
    Gecko/20100101 Firefox/27.0" \
    --save-cookies /tmp/wget-cookies.txt \
    --keep-session-cookies

但是如果我设置"$userstring"NULL

    userstring= mywget
log wget  userstring=unset cookies=save
wget --save-cookies /tmp/wget-cookies.txt --keep-session-cookies

要么:

    cookies=load cookies_file=/some/other/cookie.file mywget
log wget  userstring="Mozilla/5.0 \
    (Windows NT 6.1; WOW64; rv:27.0) \
    Gecko/20100101 Firefox/27.0" \
    cookies=load
wget --useragent="Mozilla/5.0 \
    (Windows NT 6.1; WOW64; rv:27.0) \
    Gecko/20100101 Firefox/27.0" \
    --load-cookies /some/other/cookie.file \
    --keep-session-cookies

要么:

    cookies= userstring= mywget
log wget  userstring=unset cookies=no
wget

我在这里这里都写了更多。parameter expansion

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.