1

As you can see I set up a simple script to ping google(8.8.8.8) that should respond back with "Good Ping"

if [ "ping -c 1 8.8.8.8 | grep 64 | cut -c 1-2" == '64' ]; then echo "Good Ping"; fi 

But it never responds with its echo statement

At the same time I can run:

ping -c 1 8.8.8.8 | grep 64 | cut -c 1-2 

This resolves to '64' without an issue

I don't understand the top script will never print "Good Ping", could someone explain?

0

1 Answer 1

3

The string ping -c 1 8.8.8.8 | grep 64 | cut -c 1-2 is never equal to 64, but that's what you test for. If you want to test a command, use command substitution:

if [ $(ping -c 1 8.8.8.8 | grep 64 | cut -c 1-2) == 64 ] ; then echo Good Ping fi 

`...` can be used instead of $(...).

2
  • In general it's good practice to use $() over `` as long as the shell supports it because nesting is easier that way. Commented Nov 15, 2015 at 20:08
  • ahhhh I was trying to compare that string and not its result... Thanks Choroba Commented Nov 15, 2015 at 20:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.