Help needed with conditions

Hi Folks,

Scenario:
I used the following shell script to see ping status, and got a report out of that based on the standard output value, and it worked:

for i in `cat /tmp/momin/servers`
do
echo PINGING $i....
ping $i 1
if [ $? -eq 0 ]
then
echo ping test passed!!!
echo $i >> /tmp/momin/passed
echo -------------------------------------------
else
echo ping Failed!
echo $i >> /tmp/momin/failed
echo --------------------------------------------
echo
fi
done

In the output I noticed three types of output messages:

  1. ping: unknown host tordb2
  2. no answer mondb1
  3. vandb7 is alive

The hostnames with error messages 1 and 2 went to the 'failed' report file, and the hostnames with error message 3 were directed to the 'passed' file.

Help Needed Here:
What logic can I use in my shell script / scripted command, to generate 3 files based on the 3 messages? :wall:

Thanks in advance, :slight_smile:
Momin

you can write a case statement based on pre known exit codes of the ping command. Assuming 0,1,2 are the exit code of ping you can have something like:

case $? in  
0) printf  "$i server found\n" >> file1;; 
1) printf  "$i server not found\n" >>file2;; 
2) printf  "$i server doesn't exist\n">>file3;; 
*) printf "Unknown error\n">>file4;; 
esac

Thanks :smiley: