0

I have the following script to detect when my network comes back on after restarting my router:

#!/bin/bash
pingCommand="ping 192.168.1.1 -c 3 &>/dev/null"

while [[ ! $($pingCommand) ]]; do
   sleep 3s;
done

However, when run in the terminal, it prints:

ping: unknown host &>/dev/null

I ran the script with the -x option (to enable debugging) and found that

ping 192.168.1.1 -c 3 &>/dev/null

was being executed in the subshell as

ping 192.168.1.1 -c 3 '&>/dev/null'

How do I change my command substitution call so that bash does not put single quotes around the output redirection?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

2 Answers2

7

Don't store commands in variables. Use functions. They handle redirections and pipes without the quoting issues that plague variables.

It also doesn't make sense to try to capture ping's output when you're redirecting all of that output to /dev/null. If you just want to know if it worked or not, check its exit code.

pingCommand() {
    ping 192.168.1.1 -c 3 &>/dev/null
}

while ! pingCommand; do
   sleep 3s;
done
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I am checking it's exit code, but the problem is that if there are any errors, they will display on the terminal, which I don't want. – crimsonspectre May 12 '16 at 14:10
  • They won't, because you have `&>/dev/null` which hides both stdout and stderr. – John Kugelman May 12 '16 at 14:17
  • Originally, I didn't have the `&>/dev/null`, so it was showing the errors. When I added the `&>/dev/null` to my script above, that was when it broke, hence the question haha – crimsonspectre May 12 '16 at 14:21
0

Use eval:

#!/bin/bash
pingCommand="ping 192.168.1.1 -c 3 &>/dev/null"

# set -x # uncomment to see what's going on

while ! eval $pingCommand ; do
   sleep 3s;
done

And you do not need the [[ ]] (expression evaluation) or the $() (output capture).

Of course, as John Kugelman suggested in another answer, using the functions avoids all the potential pitfalls associated with the eval.

Dummy00001
  • 16,630
  • 5
  • 41
  • 63