I have a simple script to check whether webpage contains a specified string. It looks like:
#!/bin/bash
res=`curl -s "http://www.google.com" | grep "foo bar foo bar" | wc -l`
if [[ $res == "0" ]]; then
echo "OK"
else
echo "Wrong"
fi
As you can see, I am looking to get “OK”, but got a “Wrong”.
What’s wrong with it?
If I use if [ $res == “0” ], it works. If I just use res=”0″ instead of res=curl..., it also can obtain the desired results.
Why are there these differences?
I found the answer in glenn jackman‘s help.
I get the following points in this question:
wc -l‘s output contains whitespaces.echo "$var"instead ofecho $var[[preserves the literal value of all characters within the var.[expands var to their values before perform, it’s because [ is actually thetestcmd, so it follows Shell Expansions rules.