4

given the context below..

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. results in:

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
...

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

#2. results in:

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

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. works, and i'm using it, but i do remember being chastised for using the evil/eval!

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

..hence the question.

cheers

elbeardmorez
  • 580
  • 1
  • 5
  • 13

1 Answers1

4

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"
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • thank you. it's painful that i didn't think of this. i was more focused on the issue of double-quotes than the arg array the program wanted. the way 'eval' parses a string for arguments (taking into consideration quotes/double-quotes) certainly seems more natural to me, but then i suppose, the other way, the bash way is actually a feature, one that i have used for many years without really thinking about it. – elbeardmorez Jun 08 '11 at 07:09