• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1237
  • Last Modified:

Need simple shell script to loop through a list, perform simple operation on each.

Hi Experts,

I can't quite get this one figured out. I'm trying to come up with a .pl or .sh "#bin/bash" script to loop through a list in a separate text file, e.g. one named lookups.txt, that has line by line entries like this:

host xyz.com
host cars.com
host trucks.com
host zoos.com

... then execute each "host" command and pipe the results in sequence, (for each host lookup) to a file named lookupresults.txt

So at the end of the day, I will have a text file with the results for each host lookup in the source list. Of course I could change the source lookups.txt file so that it has only domains, and no "host" command in each line. Then presumably my shell script would be in charge of running the "host" command on each domain.

At first I thought I could just past in the entire series in shell, like this:

host xyz.com > lookupresults.txt
host cars.com > lookupresults.txt
host trucks.com > lookupresults.txt
host zoos.com > lookupresults.txt

But alas, the only thing I get in the lookupresults.txt file is the results for the very last lookup made, in this example, only for "host zoos.com".

Any ideas of how I could put together such a shell script to make this work?

Thanks much.
0
uglyj
Asked:
uglyj
1 Solution
 
Darr247Commented:
A single redirect overwrites the file. Use a double redirect ( >> ) to append more data.

You could leave the first one so every time the script runs it makes the file anew.

e.g.
host xyz.com > lookupresults.txt
host cars.com >> lookupresults.txt
host trucks.com >> lookupresults.txt
host zoos.com >> lookupresults.txt
...
0
 
uglyjAuthor Commented:
Okay, that works, Good nuff. Thanks!
0
 
arober11Commented:
Or:
#!/bin/sh

echo "" > host_results.txt
for host in `cat host_file.txt`
do 
  echo  "\n\n$host" >> host_results.txt 
  host $host       >> host_results.txt 2>&1
 done

Open in new window

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now