Need help with Korn Shell script for substring printing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with Korn Shell script for substring printing
# 1  
Old 10-13-2011
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

Code:
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 ?


Code:
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

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by vbe; 10-13-2011 at 12:29 PM..
# 2  
Old 10-13-2011
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.

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

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-13-2011
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
# 4  
Old 10-13-2011
Code:
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

This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 10-14-2011
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

Code:
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.

Code:
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.
Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by vbe; 10-14-2011 at 01:28 PM.. Reason: please use code tags not font stuff
# 6  
Old 10-14-2011
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.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 10-14-2011
Thank you Corona.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Shell Extract substring

Hi all, Please, i'd like to extract string just before '.fr'. Here is some lines of my file: g-82.text.text1.fr.worker1 g-xx.yyyyyy.zzzz.fr.worker2 i'd like to extract this text: g-82.text.text1 g-xx.yyyyyy.zzzz Please, which command i have to use in my script shell ? ... (16 Replies)
Discussion started by: chercheur111
16 Replies

2. Shell Programming and Scripting

Substring check in IF condition in shell script

I want to check if the string has the substring in IF condition then process... i tried below but not working if ]; then ............. field can be "reserved1" ....reservedn / fillspaces1 ... fillspacesn (4 Replies)
Discussion started by: greenworld123
4 Replies

3. UNIX for Dummies Questions & Answers

If statement for substring within string (korn)

Hi all, Just looking for a simple if statement that searches for a substring within a varaible, and then performs some function. Problem is that I need it to work in Korn shell $var = *string* does not work in Korn i="xxxxxx00.00yyyyy.zzzzz" want to find 00.00 (2 Replies)
Discussion started by: jgrosecl
2 Replies

4. Shell Programming and Scripting

using substring in shell script

This is the data I am having in a file Just for sample I have given 3 records. The file which I am having consists of n number of records. ABC123 10 01/02/2008 2008-01-03-00.00.00.000000 DYUU 22 02/03/2008 2008-01-04-00.00.00.000000 RF33 88 03/05/2008 2008-01-05-00.00.00.000000 ... (24 Replies)
Discussion started by: kmanivan82
24 Replies

5. Shell Programming and Scripting

Substring in shell script

I need a help in getting substring of each line in input file. I am writing a script that will read a file from a directory on daily basis, I mean everyday a new file will be stored in this directory, it will replace old file. I have to read contents of this file, the contents will be as... (5 Replies)
Discussion started by: jyotib
5 Replies

6. UNIX for Dummies Questions & Answers

Substring in Shell Script

Hi I'm new to Shell scripting. Someone please help me in extracting a portion of string from a file. Eg: I got a file like, Readme.txt and has the following name value pairs input1 : /homes/input1/ input2 : /homes/input2/ ... ... When I give the parameter input1, the value... (3 Replies)
Discussion started by: smartbuddy
3 Replies

7. UNIX for Dummies Questions & Answers

problem extracting substring in korn shell

hi all, I have read similiar topics in this board, but i didn' t find the posting which is the same with the problem i face.. I try to extract string from the end. i try to do this: num=abcdefghij num2=${num:-5} echo $num2 #this should print the last 5 characters (fghij) but it doesn;t... (3 Replies)
Discussion started by: nashrul
3 Replies

8. UNIX for Dummies Questions & Answers

Substring function in UNIX shell script

Hi All, Following is the output of a find commnd to locate log directories for various projects of UNIX AIX box: /home/hbinz6pf/projectlibs/dpr_pfsdw_dev/&PH& /opt/tools/ds/Template/&PH& /data/ds/ms/hmsdw/projectlibs/dpr_ms_dev/&PH& /data/ds/riskmi/projectlibs/dpr_riskmi_dev/&PH&... (5 Replies)
Discussion started by: csrazdan
5 Replies

9. Shell Programming and Scripting

Substring function in UNIX shell script

Hi All, Following is the output of a find commnd to locate log directories for various projects of UNIX AIX box: /home/hbinz6pf/projectlibs/dpr_pfsdw_dev/&PH& /opt/tools/ds/Template/&PH& /data/ds/ms/hmsdw/projectlibs/dpr_ms_dev/&PH& /data/ds/riskmi/projectlibs/dpr_riskmi_dev/&PH&... (1 Reply)
Discussion started by: csrazdan
1 Replies

10. Shell Programming and Scripting

Substring in C shell script?

i am a new user of C-shell script. I want to know can i create a substring in a string. That means when i got a variable $input = "it is number 2" I want to get the "2" to be another variable. Can i do that in C-shell and how to ? Thank you so much dinodash (0 Replies)
Discussion started by: dinodash
0 Replies
Login or Register to Ask a Question