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?
Best Answer
Put hosts, IP addresses in a file for example.
hosts.txt
contains the followingCreate the script.
Now make the script executable and run it. Probably it defeats the purpose for what you want. Just wanted to share it.
UPDATE your answer
As a side note, your script is poorly written when it comes to bash syntax.