Need help with Korn Shell script for substring printing

Hi all,
I am new to scripting.

I have a file with colon separated values called mylist.txt

cat mylist.txt
192.123.76.89:lmprod89
162.122.20.28:lmtstserver28
10.80.32.139:hewprod139

.
.

using our internal os utility (called mvsping) we need to check all these servers if they are reachable

Our field 1 is IP and field2 is servername. I need the output of the script's to show a message like

Server : $servername "with IP "$ipaddress"is pingable"

I know how to write the IF condition part here by checking the exit status. But i need some help in looping through each lines getting field1 and printing servername(which is field2) like above if the exit statu is 0

Using korn shell, i need to write this script . Will this require a loop inside a loop ?

for iplist in `cat mylist.txt | cut -d: -f1`
do
  if
  #i will do my logic using vsping in IF condition here
  #it will be something like
  # vsping ip . if exit status = 0 , then pingable
  
  
  
  
  fi

done

That's a useless use of cat, useless use of backticks, and useless use of awk. Your code will surprise you one day unless you fix this habit.

while IFS=":" read IP NAME
do
        vsping $IP > /dev/null 2> /dev/null && echo "$IP"
done < mylist.txt > pingable.txt
1 Like

Thank you Corona for this tip.

How can i the server name(field2) printed as well?
I would like my output to look like
Server : lmprod89 with IP 192.123.76.89 is pingable

while IFS=":" read IP NAME
do
        vsping $IP > /dev/null 2> /dev/null && echo "Server : $NAME with IP $IP is pingable" 
done < mylist.txt > pingable.txt
1 Like

Thank You corona, rdcwayx
Your script worked perfectly for me.
I have one more question. Why is IFS=':' bit placed inside the WHILE loop ?

I tried placing IFS=':' outside the WHILE loop, like

IFS=":"
while  read IP NAME
do
           # MY commands

done < mylist.txt

It worked correctly for some lines, but for some lines i got the following error.

ksh[6]: vsping:     not found
ksh[3]: grep:     not found
ksh[3]: cut:     not found
ksh[3]: wc:     not found

I wasn't using grep or cut or wc. Don't know how i got these commands in the error.

THANK YOU.

IFS is a special variable controlling what characters your shell splits on. It defaults to any whitespace. By putting it outside the loop instead of where I put it, you're making that apply to all shell statements, not just read. Whatever "MY commands" was probably contained variables you forgot to quote...

IFS=":" belongs directly before read, while IFS=":" read , not on a line of its own.

1 Like

Thank you Corona.