LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-04-2008, 03:07 PM   #1
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Rep: Reputation: 33
shell script loop though text file:-1 line at a time.


Good day,

I have a file with ip address.
Code:
10.0.0.1     pc1
10.0.0.2     pc2
10.0.0.4     pc4
I have to do certain action on each of the ip address.

like
Code:
ping 10.0.0.1 
sleep 10
ping 10.0.0.2
sleep 10
ping 10.0.0.4
I know cat/awk/cut/grep treat one lie at a time, can those be used?
If yes how.

Thanks.
 
Old 05-04-2008, 03:21 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Without any of these programs, you can read a file line by line in your script using read and a while loop:
Code:
#!/bin/bash
while read ip pc
do
   ping $ip
   sleep 10
done < <(cat testfile)
Here the read command reads two fields per line from the file and uses the value $ip for the subsequent commands. The content of the file is passed to the loop using "process substitution" but you can pipe it as well:
Code:
cat testfile | while read ......
 
Old 05-04-2008, 07:58 PM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
nice post colucix .. i'll take note of the separation of fields by adding another variable :-).. gotta use these with arrays !

btw you can also use for loops like this but here you'll also use cut:
Code:
IFS_=${IFS}; IFS=$'\n'
for ip in $(cut -f 1 testfile); do
   ping $ip
   sleep 10s
done
IFS=${IFS_}
or perhaps since there are no spaces within the lines just do for
Code:
for ip in $(cut -f 1 testfile); do
   ping $ip
   sleep 10s
done

Last edited by konsolebox; 05-04-2008 at 07:59 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 03:10 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 08:15 PM
Shell script - how to show a specific line of a text file davi_cabral Linux - Software 3 09-28-2004 02:39 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 09:51 PM


All times are GMT -5. The time now is 11:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration