I have a bash script with a function that is not returning from the function as I think it should.
If the host responds to any of the 3 pings - it should exit the function with a return code 0 - otherwise it would fall out of the loop and exit with a return code 1.
What I find confusing is the trace shows executing the return 0 - but not actually doing it.
I also tried wrapping the ping with a 2nd while loop with a break 2 from the inner "while" but that didn't work as expected either.
What am I missing?
#!/bin/bash
pingCheck() {
ping -c 3 -W 1 $1 2>&1 | grep --line-buffered time= | while read a; do
return 0
done
return 1
}
set -x
pingCheck note5 || exit
...
and here is the trace output to a reachable host
+ ping -c 3 -W 1 note5
+ grep --line-buffered time=
+ read a
+ return 0
+ return 1
+ exit
and here is the trace output to a non-reachable host (works as expected)
+ ping -c 3 -W 1 note5x
+ grep --line-buffered time=
+ read a
+ return 1
+ exit