Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 IPin 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?

share|improve this question
    
What does running that netstat command output? You can have as many ifs as you want inside a loop. –  Michael Homer Jul 31 '14 at 8:22
    
Whats the output of $(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 a NOT - OK response. –  Lawrence Jul 31 '14 at 8:22
    
@Lawrence, yes is two lines. I don't think that netstat return 2 linest now on this PC. So the condition is ok and if netstat return two lines should work? –  Ivan Petrov Jul 31 '14 at 8:27
1  
Do you want to use the server ip $i somewhere in that grep command? Without using $i, the netstat|grep|grep|wc command will always output the same thing. –  Mark Plotnick Jul 31 '14 at 8:28
    
Your netstat line does not use the $i value at all. So either you'll get the "NOT - OK" line three times, or not at all. –  daniel kullmann Jul 31 '14 at 8:32

1 Answer 1

up vote 2 down vote accepted

Put hosts, IP addresses in a file for example.

hosts.txt contains the following

212.39.82.157 
212.39.82.155 
1.1.1.1
22.22.22.22

Create the script.

#!/bin/bash

ping_hosts(){
echo
echo "*** Ping all hosts ***"
echo "--------------------------------------------"

count1=0
count2=0
start=$(date +"%d-%m-%Y-%T")

hosts=( 1.1.1.1 2.3.3.4 4.5.6.6 )

while read -r line
do
#PING=`ping -s 64 $line -c 1 | grep packet | awk '{print $(NF-2)}'`
PING=$(ping -s 64 $line -c 1 | grep packet | awk '{print $(NF-4)}')

if [[ "$PING" == "0%" ]]; then
count1=$((count1 + 1))
printf '%s\n' "$line UP" 
else
count2=$((count2 + 1))
printf '%s\n\n' "$line DOWN"
fi
done < <( printf '%s\n' "${hosts[@]}")

end=$(date +"%d-%m-%Y-%T")

printf '%s\n' "Start:$start"
printf '%s\n\n' "End**:$end"
printf '%s\n' "$count1 hosts UP and $count2 hosts down"

}
ping_hosts

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

#!/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"  
  elif [ $(netstat -na | grep ':3708' | grep ESTABLISH | wc -l) -eq 0 ]; then
     echo "NOT - OK"
  fi
done

As a side note, your script is poorly written when it comes to bash syntax.

share|improve this answer
    
I'm a bit new in bash scripting and I don't understand this quite well. Can I not use second file for hosts? –  Ivan Petrov Jul 31 '14 at 8:42
    
@IvanPetrov What do you mean with a second file? You mean have hosts in two seperate files or..? –  val0x00ff Jul 31 '14 at 9:02
    
Yes, I want to have hosts in the same file. –  Ivan Petrov Jul 31 '14 at 9:04
    
@IvanPetrov see my updated answer. You can put hosts in an array variable and then read from the array just like you'd read from a file. –  val0x00ff Jul 31 '14 at 9:34
    
I've also updated my question. Can you check it? Note: My script is working fine. I just need to add second if. –  Ivan Petrov Jul 31 '14 at 9:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.