I want to create a script that compares the value of specific get request headers.
So far script content is this:
var=$(curl -I -s http://myhost.local | grep X-Magento-Cache-Debug | awk {'print $2'})
if [ "$var" = "HIT" ]; then
echo "OK"
else
echo "NOK"
exit 2
fi
So using this script I want to compare the value of X-Magento-Cache-Debug
. The problem is that the script always prints NOK
regardless the value.
It looks like the string comparison here fails although when i echo
the var
value i get the desired content.
Any ideas will be helpful thanks.
2
Answers
You are comparing the literal string
var
to the literal stringHIT
. The comparision would always be false.If you want to compare the content of the variable
var
for equality it would beHowever, this would be false if the output of your pipe does not consist just of the word
HIT
, but much more text.If you want to know, whether the content of the variable contains the substring HIT somewhere, this would be
Another possibility would be to let
grep
do the job:Alternative 1:
Test your condition directly in awk:
Alternative 2:
Use safer and more flexible pattern matching with
[[ =~ ]]
or …
if [[ $conn =~ keep ]]; then foobar ; fi