LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Reading line from a file and process with bash script (https://www.linuxquestions.org/questions/linux-newbie-8/reading-line-from-a-file-and-process-with-bash-script-934643/)

moyorakkhi 03-15-2012 02:50 PM

Reading line from a file and process with bash script
 
Hello,

I need little help with a bash script. I want to transfer zone information from TinyDNS to BIND. I'm running axfrdns on TinyDNS server. So I'm using dig axfr to get the info from tinydns as bind data format and keep the output in a file. I have 400 domains so doing this for each domain is tedious. I want to automate with bash script. I have all the domain names in a file. How can I read each line from that file and use it? I did the following but it's only giving output of the last line.

Code:

#!/bin/bash

for i in `cat test.txt`; do echo $i; done

dig -p 54 @192.168.0.3 $i axfr > $i.hosts


druuna 03-15-2012 02:58 PM

Hi,

The dig command needs to be part of the loop, which it isn't at the moment.

You first loop through the content of the test.txt file and print the content line by line:
Code:

for i in `cat test.txt` ; do echo $i; done
When that is done you execute the dig command, once and with the last entry from the test.txt file.

Try this:
Code:

#!/bin/bash

for i in $(cat test.txt)
do
  echo $i
  dig -p 54 @192.168.0.3 $i axfr > $i.hosts
done > $i.hosts

As you might notice that the way it was written above gives you a better view of what is and isn't part of the loop.

Hope this helps.

EDIT: Redirecting to differently named outfiles must also be done inside the loop

David the H. 03-15-2012 03:10 PM

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Also, Don't Read Lines With For, and Useless Use Of Cat.

Code:

while read i; do

        echo "$i"
        dig -p 54 @192.168.0.3 "$i" axfr

done <test.txt >"$i.hosts"


Finally, QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-split by the shell. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

---------- Post added 2012-03-16 at 05:10 AM ----------

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Also, Don't Read Lines With For, and Useless Use Of Cat.

Code:

while read i; do

        echo "$i"
        dig -p 54 @192.168.0.3 "$i" axfr

done <test.txt >"$i.hosts"


Finally, QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-split by the shell. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes



Edit: There's been an odd duplication of my text. Some kind of posting bug. I'm leaving it as-is for the time being while I try to report it.

moyorakkhi 03-15-2012 03:30 PM

Thanks a lot dudes!! @druuna: it worked nicely. @David: Thanks for the guideline. BTW, when I executed your suggested code it was returning,
Code:

dig: '' is not a legal name (unexpected end of input)
.

David the H. 03-15-2012 04:07 PM

That error message is given by dig, which I have no experience with. There are no obvious shell syntax errors that I can see. Perhaps you're getting an empty value for "$i"? In which case you'd need to set up a test first to skip blank entries.

I have however spotted a different error that I missed before.

Since ">$i.hosts" sits outside of the loop, "$i" isn't defined when the redirection is set up, and the resulting filename will be simply ".hosts" (unless there's a previously-defined value for "$i", of course, in which case it uses that).

We have to move the redirection inside the loop to get the proper behavior.

Code:

while read i; do

        [[ -z $i ]] && continue

        echo "$i" >>"$i.hosts"
        dig -p 54 @192.168.0.3 "$i" axfr >>"$i.hosts"

done <test.txt

This should append the output of both commands to a filename starting with the current value of "$i".

moyorakkhi 03-16-2012 04:33 AM

Great! The new one worked. Thanks!!


All times are GMT -5. The time now is 05:37 PM.