如何让嵌套双引号生存的bash间preTER? [英] how to make nested double quotes survive the bash interpreter?

查看:114
本文介绍了如何让嵌套双引号生存的bash间preTER?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

..

确实任何魔术语法存在,可以在$ WGETOPT的定义被插入以允许$ USERAGENT变量是'吸收',并且仍然允许用于向wget命令调用如<强>语法1 ?目前我已经开始借助EVAL我不是很满意,但也许这是我能做些什么我想在这里的唯一途径?!

does any magic syntax exist that can be inserted in the definition of $WGETOPT to allow the $USERAGENT variable to be 'absorbed', and still allow for a call to the wget command as in syntax 1? i've currently resorted to using 'eval' which i'm not happy with, but maybe this is the only way i can do what i want to here?!

#params
USERAGENT="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
PROXYSWITCH=off
WGET=wget
WGETOPT="--cut-dirs=3 -r -l10 -dnv -x -H --timestamping --limit-rate=100K --proxy=$PROXYSWITCH -U \"$USERAGENT\"" 
WGETOPTminusUSERAGENT="-cut-dirs=3 -r -l10 -dnv -x -H --timestamping --limit-rate=100K --proxy=$PROXYSWITCH" 
URL=http://blah

#commands
#1.
    $WGET $WGETOPT $URL
#2.
    $WGET "$WGETOPT" $URL
#3.
    eval "$WGET $WGETOPT $URL"
#4. 
    $WGET $WGETOPTminusUSERAGENT -U "$USERAGENT" $URL

1 即可。结果是:

DEBUG output created by Wget 1.11.4 on Windows-MinGW.
Enqueuing http://(compatible;/ at depth 0
Queue count 1, maxcount 1.
Host `(compatible;' has not issued a general basic challenge.
...
Setting --user-agent (useragent) to "Mozilla/4.0
...

这是显而易见的,为什么,在\\没有'生存'bash的跨preTER。

it's obvious why, the "\" didn't 'survive' the bash interpreter.

2 。结果是:

wget: --cut-dirs: Invalid number `3 -r -l10 -dnv -x -H --timesta.. ..indows NT 5.1)"'.'

在这里,双引号导致传递作为唯一参数,wget的分析从命名参数,然后假设(正确地)剩下的就是它的参数,无论空白。

here, the double-quotes result in a single argument being passed which wget parses a named parameter from, and then assumes (correctly) that the rest is its argument, regardless of white space.

#3 。作品,我用它,但我记得被严厉谴责使用邪恶/ EVAL!

#3. works, and i'm using it, but i do remember being chastised for using the evil/eval!

#4 。我假设的作品很好,我没有测试它,因为这不是我想怎么办呢!

#4. i'll assume works fine, i didn't test it because that's not how i want to do it!!

..因此问题。

欢呼声

推荐答案

要放大@伊格纳西奥的回答:如果我了解这里的目标,最好的答案是存储在一个阵列中的选项

To amplify @Ignacio's answer: if I understand the goal here, the best answer is to store the options in an array.

#params
USERAGENT="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
PROXYSWITCH=off
WGET=wget
WGETOPT=(--cut-dirs=3 -r -l10 -dnv -x -H --timestamping --limit-rate=100K --proxy=$PROXYSWITCH -U "$USERAGENT")

$WGET "${WGETOPT[@]}" "$URL"

这篇关于如何让嵌套双引号生存的bash间preTER?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆