Hi there,
I need to do something if a ping fails:
if ping -c1 192.168.1.1 > /dev/null 2>&1; then :; else
echo Ping failed
fi
Can't I just do something like:
if not ping -c1 192.168.1.1 > /dev/null 2>&1; then
echo Ping failed
fi
Obviously this doesn't work. What's the syntax for if not?
Thanks for your help
Santiago
you have to do something like this...
ping -c 2 -s 100 -w 1 ipaddress >/dev/null
if [ $? -eq 1 ] ; then
echo "ping failed"
else
echo "pinging"
fi
rikxik
3
$ ping 192.168.1.1
no answer from 192.168.1.1
$ if ! ping -c1 192.168.1.1 > /dev/null 2>&1; then
> echo ping failed
> fi
ping failed
$ ping 10.68.64.122
10.68.64.122 is alive
$ if ! ping -c1 10.68.64.122 > /dev/null 2>&1; then
> echo ping failed
> fi
$
Thanks vidyadhar for your time and help.
Thanks rikxik for your prop, that's exactly what I was looking for.
rikxik
5
If you are using ksh, you can use the short circuit operator:
ping -c1 192.168.1.1 > /dev/null 2>&1 || echo "ping failed"
Thanks rikxik,
I knew this short circuit and I'm using it with bash.
But I needed your syntax because I have several lines in the if then.