uglyj
asked on
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER