我正在编写一个广泛使用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" "$@"
}
感谢您的所有回复。