Linux 在 bash 中设置 wget --header= 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18063679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:28:09  来源:igfitidea点击:

Setting wget --header= in bash doesn't work

linuxbashwget

提问by rabotalius

I trying to set header in wget. When I run the following command in terminal it works wget -d --header="User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"' http://website.com -O index

我试图在 wget 中设置标题。当我在终端中运行以下命令时,它可以工作wget -d --header="User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"' http://website.com -O index

but once i put the same in variables and try to run a bash script it's not working.

但是一旦我将相同的变量放在变量中并尝试运行 bash 脚本,它就不起作用了。

what i've tried

我试过的

header='-d --header="User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"'
wget "$header" http://google.com -O index

error

错误

wget: invalid option -- ' '
wget: invalid option -- '-'
wget: invalid option -- '-'
Usage: wget [OPTION]... [URL]...

回答by nosid

You have to use double quotes when using the variable. Otherwise it will be expanded into multiple words. On the other hand, there is no need to quote the variable value twice. The following should work:

使用变量时必须使用双引号。否则它会被扩展为多个单词。另一方面,不需要两次引用变量值。以下应该工作:

header='--header=User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11'
wget "$header" http://website.com -O index

Edit:If you want to use variables for several arguments, use arrays:

编辑:如果要对多个参数使用变量,请使用数组:

args=(-d '--header=User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11')
wget "${args[@]}" http://website.com -O index

回答by SSaikia_JtheRocker

Instead of this

而不是这个

wget $header http://website.com -O index

try this,

尝试这个,

wget "$header" http://website.com -O index

The spaces in the header text is breaking up when you are assigning it the header variable. To do away with the problem you must surround a variable with quotes "".

当您将标题变量分配给标题文本时,标题文本中的空格会破裂。要解决这个问题,您必须用引号 "" 将变量括起来。

After comments:Try this -

评论后:试试这个 -

someheader="-d --header='User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11'"
wget "$someheader" http://website.com -O index

The name header seems conflicting with your --header. Or, may be the quotes, instead of copying type them and retry. Still, Weird!

名称标题似乎与您的 --header 冲突。或者,可能是引号,而不是复制它们并重试。还是,怪!

回答by WxWizard

COMMENT: 'nosid's trick about the bash array worked for me. In my case the relevant code is:

评论:'nosid 关于 bash 数组的技巧对我有用。就我而言,相关代码是:

WGET_OPTS="-r -N -nd -np -nH --timeout=120 --tries=3"
WGET_OPTS_ARRAY=(${WGET_OPTS// / })
wget "${WGET_OPTS_ARRAY[@]}" -A "$FILE_PAT" -P "$TO_DIR" "$FROM_URL"

回答by Michael Meepo

What you only need to do is to escape -dlike this: header='\-d --header="User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"'

你只需要-d像这样逃跑: header='\-d --header="User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"'