我正在编写一个广泛使用 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 上存储参数的正确方法,以及将这些变量作为参数传递的 caling 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" "$@"
}
感谢您的所有回复。
答案1
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"
引用了 。这一点至关重要。
答案2
问题在于扩展的顺序(man bash
详细信息请参阅 EXPANSION )。当变量展开时,变量内的双引号不能用作双引号:
$ 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
答案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