I've trying to make two if conditions in for loop. Is it possible this? Now doesn't return anything from second if
only two OK
from first if
.
#!/bin/bash
servers=("212.39.82.157" "212.39.82.157" "1.1.1.1")
for i in "${servers[@]}"; do
ping -c 1 $i > /dev/null
if [ $? -eq 0 ]; then
echo "OK"
fi
if [ $(netstat -na | grep ':3708' | grep ESTABLISH | wc -l) -eq 2 ]; then
echo "NOT - OK"
fi
done
For third IP
in the list must return NOT - OK
since is not online. But the output is this
root@ubuntu:~$ ./check.sh
OK
OK
root@ubuntu:~$
What I missing here?
UPDATE:
#!/bin/bash
servers=("212.39.82.157" "212.39.82.157" "1.1.1.1")
for i in "${servers[@]}"; do
ping -c 1 $i > /dev/null
if [ $? -eq 0 ]; then
echo "OK"
fi
done
if [ $(netstat -na | grep ':3708' | grep ESTABLISH | wc -l) -eq 0 ]; then
echo "NOT - OK"
fi
If I put it outside for loop it must work?
netstat
command output? You can have as manyif
s as you want inside a loop.$(netstat -na | grep ':3708' | grep ESTABLISH | wc -l)
within the script ? Is it only 2 lines ? I would echo it out to double check whether it's 2 lines or not. If it's not 2 lines, then you won't get aNOT - OK
response.netstat
return 2 linest now on this PC. So the condition is ok and ifnetstat
return two lines should work?$i
value at all. So either you'll get the "NOT - OK" line three times, or not at all.