Reputation: 10284
I have the following command which, if fired with a hardcoded IP works fine -
ad_request_output="$(/usr/local/nagios/libexec/check_http -w 0.18 -c 0.25 -H <Some private IP> -u 'http://<Some private IP>/fam/postGetAd.php?site_id=76986&partner_id=27de34b6f8b03d81&banner_id=183517&timeout=5000&version=1.5.1&language=jsp&format=wap&phone_headers=REMOTE_ADDR=>166.137.8.134||REMOTE_HOST=>http://localhost||HTTP_USER_AGENT=>Mozilla/5.0')"
echo $ad_request_output
gives expected output -
HTTP OK: HTTP/1.1 200 OK - 217 bytes in 0.055 second response time |time=0.054961s;0.180000;0.250000;0.000000 size=217B;;;0
But, using a variable IP gives a different output -
private_ip=<Some private IP>
ad_request_output=$(/usr/local/nagios/libexec/check_http -w 0.18 -c 0.25 -H $private_ip -u 'http://$private_ip/fam/postGetAd.php?site_id=76986&partner_id=27de34b6f8b03d81&banner_id=183517&timeout=5000&version=1.5.1&language=jsp&format=wap&phone_headers=REMOTE_ADDR=>166.137.8.134||REMOTE_HOST=>http://localhost||HTTP_USER_AGENT=>Mozilla/5.0')
echo $ad_request_output
gives -
HTTP WARNING: HTTP/1.1 400 Bad Request - 311 bytes in 0.001 second response time |time=0.000703s;0.180000;0.250000;0.000000 size=311B;;;0
Tried with this format of putting variable ${private_ip}
as well, but got same output -
ad_request_output=`/usr/local/nagios/libexec/check_http -w 0.18 -c 0.25 -H ${private_ip} -u 'http://${private_ip}/fam/postGetAd.php?site_id=76986&partner_id=27de34b6f8b03d81&banner_id=183517&timeout=5000&version=1.5.1&language=jsp&format=wap&phone_headers=REMOTE_ADDR=>166.137.8.134||REMOTE_HOST=>http://localhost||HTTP_USER_AGENT=>Mozilla/5.0'`
I checked some related questions Bash - Using variable inside command not working but got no clue what am I doing wrong.
I have working code to use variable in a command -
php /var/cake_1.2.0.6311-beta/beforeInstall.php ${OUTPUT}
But, not sure how to do it when the output needs to be collected in a variable.
Upvotes: 1
Views: 127
Reputation: 686
Oliv already answered your question correctly. But I would like to add some help so that you can find the answer yourself in the future.
Given your script to begin with (I simplified the command):
#!/bin/bash
private_ip=127.0.0.1
ad_request_output=$(check_http -H $private_ip -u 'http://$private_ip/fam/postGetAd.php')
echo $ad_request_output;
You could simply call bash with the parameter -x, which prints every command before it is executed
bash -x test.sh
Output:
adiesner@local /tmp> bash -x test.sh
+ private_ip=127.0.0.1
++ check_http -H 127.0.0.1 -u 'http://$private_ip/fam/postGetAd.php'
t.sh: Line 4: check_http: Command not found
[...]
You can clearly see that $private_ip was replaced once, but not the second time.
Another way is to simply output the command by placing "echo" in front of it.
#!/bin/bash
private_ip=127.0.0.1
ad_request_output=$(echo check_http -H $private_ip -u 'http://$private_ip/fam/postGetAd.php')
echo $ad_request_output;
Output:
adiesner@local /tmp> ./test.sh
check_http -H 127.0.0.1 -u http://$private_ip/fam/postGetAd.php
As soon as you know what is going on it should be easy to enter the correct search words.
Upvotes: 1
Reputation: 12373
In bash
all characters inside '
are preserved. That means that $<variable>
is not expanded. See this:
$ something=value
$ echo $something
value
$ echo "$something"
value
$ echo '$something'
$something
In your specific case $private_ip
would not be expanded to the value of private_ip
. Relevant section from man bash
:
Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
Upvotes: 1
Reputation: 13239
The private_ip
variable doesn't get expanded if you use single quote. You should use double quotes:
ad_request_output=$(/usr/local/nagios/libexec/check_http -w 0.18 -c 0.25 -H ${private_ip} -u "http://${private_ip}/fam/postGetAd.php?site_id=76986&partner_id=27de34b6f8b03d81&banner_id=183517&timeout=5000&version=1.5.1&language=jsp&format=wap&phone_headers=REMOTE_ADDR=>166.137.8.134||REMOTE_HOST=>http://localhost||HTTP_USER_AGENT=>Mozilla/5.0")
Upvotes: 1