LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash: Can't get wget to work with vars (http://www.linuxquestions.org/questions/linux-general-1/bash-cant-get-wget-to-work-with-vars-4175448361/)

carters2 02-02-2013 12:56 PM

Bash: Can't get wget to work with vars
 
I am having a real tough time getting wget to work when using variables. I have gotten it to work by creating the url manually but when I start using variables I simply cannot get it to work.

#!/bin/bash -x

# Working - Hard Coded
echo "fisrt"
wget --no-check-certificate 'https://127.0.0.1/job/deploy-horizon-service-va-core/api/xml?depth=1&xpath=/freeStyleProject/build[action/cause/upstreamProject="deploy-serv-conn-core" and action/cause/upstreamBuild="1238"]/number/text()' -O hardCoded.out

# With Vars
JOB=deploy-serv-conn-core
BUILD=1238
echo "second"
wget --no-check-certificate "https://127.0.0.1/job/deploy-horizon-service-va-core/api/xml?depth=1&xpath=/freeStyleProject/build[action/cause/upstreamProject='${BUILD}' and action/cause/upstreamBuild='${JOB}']/number/text()" -O withVars.out
echo "third"
wget --no-check-certificate "https://127.0.0.1/job/deploy-horizon-service-va-core/api/xml?depth=1&xpath=/freeStyleProject/build[action/cause/upstreamProject='${BUILD}' and action/cause/upstreamBuild='${JOB}']/number/text()" -O withVars.out
echo "fourth"
wget --no-check-certificate "https://127.0.0.1/job/deploy-horizon-service-va-core/api/xml?depth=1&xpath=/freeStyleProject/build[action/cause/upstreamProject="${BUILD}" and action/cause/upstreamBuild="${JOB}"]/number/text()" -O withVars.out

OUTPUT

+ echo fisrt
fisrt
+ wget --no-check-certificate 'https://127.0.0.1/job/deploy-horizon-service-va-core/api/xml?depth=1&xpath=/freeStyleProject/build[action/cause/upstreamProject="deploy-serv-conn-core" and action/cause/upstreamBuild="1238"]/number/text()' -O hardCoded.out
--2013-02-02 13:54:00-- https://127.0.0.1/job/deploy-horizon...eProject/build[action/cause/upstreamProject=%22deploy-serv-conn-core%22%20and%20action/cause/upstreamBuild=%221238%22]/number/text()
Resolving 127.0.0.1 (127.0.0.1)... 127.0.0.1
Connecting to 127.0.0.1 (127.0.0.1)|127.0.0.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4 [text/plain]
Saving to: `hardCoded.out'

100%[==================================================================================================== =======================================================>] 4 --.-K/s in 0s

2013-02-02 13:54:00 (187 KB/s) - `hardCoded.out' saved [4/4]

+ JOB=deploy-serv-conn-core
+ BUILD=1238
+ echo second
second
+ wget --no-check-certificate 'https://127.0.0.1/job/deploy-horizon-service-va-core/api/xml?depth=1&xpath=/freeStyleProject/build[action/cause/upstreamProject='\''1238'\'' and action/cause/upstreamBuild='\''deploy-serv-conn-core'\'']/number/text()' -O withVars.out
--2013-02-02 13:54:00-- https://127.0.0.1/job/deploy-horizon...eProject/build[action/cause/upstreamProject='1238'%20and%20action/cause/upstreamBuild='deploy-serv-conn-core']/number/text()
Resolving 127.0.0.1 (127.0.0.1)... 127.0.0.1
Connecting to 127.0.0.1 (127.0.0.1)|127.0.0.1|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2013-02-02 13:54:00 ERROR 404: Not Found.

+ echo third
third
+ wget --no-check-certificate 'https://127.0.0.1/job/deploy-horizon-service-va-core/api/xml?depth=1&xpath=/freeStyleProject/build[action/cause/upstreamProject='\''1238'\'' and action/cause/upstreamBuild='\''deploy-serv-conn-core'\'']/number/text()' -O withVars.out
--2013-02-02 13:54:00-- https://127.0.0.1/job/deploy-horizon...eProject/build[action/cause/upstreamProject='1238'%20and%20action/cause/upstreamBuild='deploy-serv-conn-core']/number/text()
Resolving 127.0.0.1 (127.0.0.1)... 127.0.0.1
Connecting to 127.0.0.1 (127.0.0.1)|127.0.0.1|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2013-02-02 13:54:00 ERROR 404: Not Found.

+ echo fourth
fourth
+ wget --no-check-certificate 'https://127.0.0.1/job/deploy-horizon-service-va-core/api/xml?depth=1&xpath=/freeStyleProject/build[action/cause/upstreamProject=1238 and action/cause/upstreamBuild=deploy-serv-conn-core]/number/text()' -O withVars.out
--2013-02-02 13:54:00-- https://127.0.0.1/job/deploy-horizon...eProject/build[action/cause/upstreamProject=1238%20and%20action/cause/upstreamBuild=deploy-serv-conn-core]/number/text()
Resolving 127.0.0.1 (127.0.0.1)... 127.0.0.1
Connecting to 127.0.0.1 (127.0.0.1)|127.0.0.1|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2013-02-02 13:54:00 ERROR 404: Not Found.

trey85stang 02-02-2013 02:13 PM

escape your single quotes. bash interprets things inside of single quotes literally.

Code:

$ myvar="blah blah"
$ echo $myvar
blah blah
$ echo "$myvar"
blah blah
$ echo '$myvar'
$myvar
$ echo \'$myvar\'
'blah blah'



All times are GMT -5. The time now is 02:40 PM.