I'm making a small script to determine if I have an internet connection on OSX. More of just practice, I suppose.
In terminal "ifconfig | grep -cs 'status: active' " will return 1 if there's at least one active connection
The script I have is this
#!/bin/bash
detect(){
ONLINE=ifconfig | grep -cs 'status: active'
}
if [[ detect = 1 ]]
then
echo "Online"
else
echo "Offline"
fi
However the Variable ONLINE always returns 0. From what I can tell/understand, this has to do with using a pipe inside of the script. A sub-pipe is used when running the command, and ONLINE just gets stuck with 0 as the sub-pipe closes.
I think I see the issue, but I don't know how to get around this. I saw a bunch of work arounds for scripts having this issue with while loops, but nothing where I need the output from ifconfig fed into grep.
use this:
cause without "$" what bash will return is the result of the command being successful or not and if it is successful it is always zero.