LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-15-2012, 03:50 PM   #1
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Rep: Reputation: 1
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

Last edited by moyorakkhi; 03-15-2012 at 04:26 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-15-2012, 03:58 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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

Last edited by druuna; 03-15-2012 at 04:05 PM.
 
2 members found this post helpful.
Old 03-15-2012, 04:10 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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.

Last edited by David the H.; 03-15-2012 at 04:20 PM.
 
1 members found this post helpful.
Old 03-15-2012, 04:30 PM   #4
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Original Poster
Rep: Reputation: 1
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)
.
 
Old 03-15-2012, 05:07 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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".
 
Old 03-16-2012, 05:33 AM   #6
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Original Poster
Rep: Reputation: 1
Great! The new one worked. Thanks!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script only appears to process one line of file Nylex Programming 5 08-19-2007 11:35 PM
Need help reading text file in bash script scilec Programming 3 11-25-2004 07:44 PM
Bash script - reading from text file twantrd Programming 4 11-24-2004 01:38 AM
reading file, bash script marri Programming 3 11-15-2004 10:13 AM


All times are GMT -5. The time now is 01:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration