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

Posted on 2012-09-03
Last Modified: 2012-09-03
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:


... 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 > lookupresults.txt
host > lookupresults.txt
host > lookupresults.txt
host > 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".

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

Thanks much.
Question by:uglyj
    LVL 44

    Accepted Solution

    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.

    host > lookupresults.txt
    host >> lookupresults.txt
    host >> lookupresults.txt
    host >> lookupresults.txt

    Author Closing Comment

    Okay, that works, Good nuff. Thanks!
    LVL 26

    Expert Comment

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

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension ( This reminded me of questions tha…
    Join Greg Farro and Ethan Banks from Packet Pushers ( and Greg Ross from Paessler ( for a discussion about smart network …
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now