What's the best way to do this..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What's the best way to do this..
# 1  
Old 09-21-2008
What's the best way to do this..

I want to create a script using sh to read a text file with a list of IP's and run a single command with that list on each. I would like my script to grab the IP as a variable so that I can fiddle with it in the script but I'm not sure of how to go about getting the script to 'read' each line then run the command and loop for each entry.

So for example...

Command would be something like this:
dig @abc.com axf 123.com | grep '$ip'

and the text file would contain something like...

1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4


Please help. I'm novice at scripting so I'm sure there's a word for it but I don't know what it is to do the necessary search here in the forum. Please advise on how best to implement (array,sed,awk, exec, etc. etc.). Not looking to get spoon fed, just looking for some direction.

Smilie
# 2  
Old 09-21-2008
Use a while loop like this:

Code:
while read ip; do
  # Do something with "$ip"
done < text_file

Regards
# 3  
Old 09-22-2008
Looking to capture each ip then loop like so..


#!/bin/sh
while read IP
do
dig @server1.com axfr server2.com | grep "$ipaddr" ;

^^do again with the next line, stdout
^^do again with the next line, stdout
^^do again with the next line, stdout
--end of file
echo "done."

Not sure of how to get $ipaddr from the file for each line though.
# 4  
Old 09-22-2008
Hammer & Screwdriver Unsure what trying to do, but here is a loop

> cat file1
1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4

> cat my_loop
#! /usr/bin/bash

while read zf
do
echo "Processing for $zf"
# dig @server1.com axfr server2.com $zf

done <file1

> my_loop
Processing for 1.1.1.1
Processing for 2.2.2.2
Processing for 3.3.3.3
Processing for 4.4.4.4
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question