String comparisons

Can someone please tell me what is wrong with this stings comparison?

#!/bin/sh 
#set -xv
set -u
VAR=$(ping -c 5 -w 10 google.com | grep icmp_req=5 | awk '{print $6}')
echo I like cookies
echo $VAR
if "$VAR" == 'icmp_req=5'
then
echo You Rock
else
echo You Stink
fi

This is the error. I know its not a command. I don't understand why it thinks it is. I have tried single quotes, double quotes, single brackets, and double brackets with no luck.

$ bash ./ping
I like cookies
icmp_req=5
./ping: line 7: icmp_req=5: command not found
You Stink

mising brackets

if [ "$VAR" == 'icmp_req=5' ] 
if [ "$VAR" == 'icmp_req=5' ]

I tried with both single brackets and double brackets but I still get that message.

#!/bin/sh 
#set -xv
set -u
VAR=$(ping -c 5 -w 10 google.com | grep icmp_req=5 | awk '{print $6}')
echo I like cookies
echo $VAR
if ["$VAR" == 'icmp_req=5']
then
echo You Rock
else
echo You Stink
fi

$ bash ./ping
I like cookies
icmp_req=5
./ping: line 7: [icmp_req=5: command not found
You Stink

you missed space after [ and before ]

1 Like

Thank you. I have been trying to figure that out for like the last hour. Silly/Frustrating mistake.

If all that interests you is success or failure, you can just test its exit status directly in the if-statement.

if ping -c 5 -w 10 google.com >/dev/null; then
    <ping succeeded>
else
    <ping failed>
fi

Regards,
Alister