I wrote this bash script to have a notification when internet becomes accessible but I don't understand many of things that are happening. Here is the script:
while ! ping 8.8.8.8 -c 1 2&> /dev/null ; do true; done;
- The
-c 1
option tellsping
that i want to send only one packet. - The
2&> /dev/null
is there because i don't want to see output. true
is there becausebash
didn't accept thedo;
syntax.- the terminating condition is
! ping ...
becauseping
returns non zero status code when no packet is received before sometimeout
.
The intended behavior was for this loop to end when ping
succeeds and the terminal emulator will automatically send me a notification.
Problems
- This command doesn't end even when internet becomes accessible.
- Trying to kill the command with
ctrl + C
doesn't work. I think it's killing the sub-commandping
instead of the whole command. - Only removing
2&> /dev/null
makes every thing works except for the minor issue that it writes output to the terminal.
Questions
- What's wrong with my command?
- How exactly do terminal emulators react to
ctrl + C
? - Why does removing
2&> /dev/null
make it work?
Note that I already have a turnaround. Nevertheless I want to understand bash
a little more.
f() { while ! ping 8.8.8.8 -c 1 ; do true; done; }; f 2&> /dev/null