LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash: escaping/anchoring args for wget (https://www.linuxquestions.org/questions/linux-newbie-8/bash-escaping-anchoring-args-for-wget-4175478891/)

ezekieldas 09-28-2013 12:43 PM

bash: escaping/anchoring args for wget
 
I use the following wget foo:
Code:

wget -qO - --no-check-certificate --header='Content-Type: text/plain' --header='Accept: text/plain' http://snoop.nsa.gov/snooplist.txt
I want to put the args and stuff in variables and call it up this way:

Code:

wget=/usr/local/bin/wget
wget_args="-qO - --no-check-certificate --header='Content-Type: text/plain' --header='Accept: text/plain'"
url=http://snoop.nsa.gov/snooplist.txt

...but when I call $wget $wget_args $url the header stuff is never passed correctly because it's losing the ticks around 'Content-Type: text/plain'.

I have tried many variations here, enclosing the whole wget_args in ticks, using anchors (escaping) before each tick, and so on. Rather than illustrating all of my failed attempts can someone show me how this might be handled properly?

grail 09-28-2013 01:04 PM

Try placing the items in an array and calling it:
Code:

wget=/usr/local/bin/wget
wget_args=( -qO - --no-check-certificate --header='Content-Type: text/plain' --header='Accept: text/plain' )
url=http://snoop.nsa.gov/snooplist.txt

$wget ${wget_args[*]} $url


ezekieldas 09-30-2013 03:54 PM

Grail --that's a smart approach but it still loses the ticks around the header values and as a result they are never passed to the server:

If I run with set -x

+ /usr/local/bin/wget -qO - --no-check-certificate --header=Content-Type: text/plain --header=Accept: text/plain http://snoop.nsa.gov/snooplist.txt

grail 10-01-2013 04:31 AM

So it is failing? Not sure if the address is made up, but it does not work for me copying it from your original post anyway.

ezekieldas 10-01-2013 11:20 AM

The url I'm using is just a humorous example. What I'm actually using it against is an (internal access only) API that gives either JSON or TEXT responses based on the requested header. Unfortunately I can't find a site that behaves similarly that I could post here as an example.

Yet if you run the following you'll see how the first wget does not include ticks around the header values but the second one does.

I've not yet found a means to maintain those header values...


Code:

set -x

wget=/usr/local/bin/wget
wget_args=( -qO - --no-check-certificate --header='Content-Type: text/plain' --header='Accept: text/plain' )
url=http://news.google.com/intl/en_us/about_google_news.html

$wget ${wget_args[*]} $url >/dev/null

wget -qO - --no-check-certificate --header='Content-Type: text/plain' --header='Accept: text/plain' http://news.google.com/intl/en_us/about_google_news.html >/dev/null

Code:

+ wget=/usr/local/bin/wget
+ wget_args=(-qO - --no-check-certificate --header='Content-Type: text/plain' --header='Accept: text/plain')
+ url=http://news.google.com/intl/en_us/about_google_news.html
+ /usr/local/bin/wget -qO - --no-check-certificate --header=Content-Type: text/plain --header=Accept: text/plain http://news.google.com/intl/en_us/about_google_news.html
+ wget -qO - --no-check-certificate '--header=Content-Type: text/plain' '--header=Accept: text/plain' http://news.google.com/intl/en_us/about_google_news.html


grail 10-01-2013 12:10 PM

Ok ... I was using a different method so missed what you needed initially, but what if you quote within the array:
Code:

wget_args=( -qO - --no-check-certificate "--header='Content-Type: text/plain'" "--header='Accept: text/plain'" )
This keeps the quotes but then the issue will be on how wget interprets what is passed to it??


All times are GMT -5. The time now is 05:14 PM.