LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 05-30-2010, 10:47 AM   #1
yathin
LQ Newbie
 
Registered: May 2010
Location: India-Bangalore
Posts: 2

Rep: Reputation: 0
nested loop-bash script- issue on logic


Hi,

Am new in bash scripting, presently I have 2 files and i need to create a file reading from these 2 files. I was trying to work with 2 for loops, but I can see like once its executing 1st outer loop then all inner loop, then next outer loop+ all inner loop. How can i get result like 1 outer loop, 1 inner loop then 2 outer loop,2 inner loop etc. Below is my prog

#!/bin/bash
rm d
for i in `cat a`
do
echo "dn:$i >> d

for j in `cat b`
do
echo "mail: $j" >> d
done
done
echo >> d


Thanks,
 
Old 05-30-2010, 11:18 AM   #2
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 70
Its getting like a homework club again, thank God for the summer holidays, eh?

Though I should ask, is this homework? Because it really does sound like it.
 
Old 05-30-2010, 11:55 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Shell scripting is sequential by nature. With rare exceptions it only runs one command at a time; command 2 starts when command 1 finishes, and so on. If a command is a loop, then the whole loop is run, as you've discovered.

What you really need to do is figure out how to grab the lines from both of the files in a way that allows them to be processed in a single loop. One suggestion would be to first load one or both files into arrays, so you can call the individual lines by their index numbers. Another option would be to use something like sed or grep to fetch the appropriate line from the second file when iterating through the first one.

By the way, it's generally better to use a while loop with read and I/O redirection than a for loop with `cat a`. Also, "$()" is usually recommended over the old style `` backticks. For example:
Code:
while read filename; do
	echo "$(file $filename)" >>newfile.txt
done <list_of_filenames.txt
Finally, please use [code][/code] tags when posting, to preserve formatting and improve readability.
 
Old 05-30-2010, 01:59 PM   #4
fruttenboel
Member
 
Registered: Jul 2008
Location: Tilburg NL
Distribution: Slackware 14.2 ciurrent, kernel 3.18.11
Posts: 270

Rep: Reputation: 48
Quote:
Originally Posted by yathin View Post
Hi,

Am new in bash scripting, presently I have 2 files and i need to create a file reading from these 2 files. I was trying to work with 2 for loops, but I can see like once its executing 1st outer loop then all inner loop, then next outer loop+ all inner loop. How can i get result like 1 outer loop, 1 inner loop then 2 outer loop,2 inner loop etc. Below is my prog

#!/bin/bash
rm d
for i in `cat a`
do
echo "dn:$i >> d

for j in `cat b`
do
echo "mail: $j" >> d
done
done
echo >> d


Thanks,
Baqsh scripts are for specific tasks. In this case, use a proper language, like java, Tcl/Tk or even Python. In the end you will be the big winner: you've mastered a language, not a scipting tool.
 
Old 05-30-2010, 10:50 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by fruttenboel View Post
Baqsh scripts are for specific tasks. In this case, use a proper language, like java, Tcl/Tk or even Python. In the end you will be the big winner: you've mastered a language, not a scipting tool.
I completely disagree here. Pretty much all programming is for solving "specific tasks". Shell scripting is just another tool in your box that can be called on in order to solve them (and a much more powerful one than you seem to give it credit for). Every serious user should learn at least the basics of shell scripting, and who can honestly call himself a programmer if he can't whip out a decent script when needed?

Besides, "I have 2 files and i need to create a file reading from these 2 files." is a "specific task" that's perfect for scripting. The only problem here is in the design of the flow control, not which language is being used.
 
Old 05-31-2010, 01:24 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Another option is joining the two files side-by-side using the paste command, then reading from the output using a single while loop. The only issue is to choose an appropriate delimiter that does not appear in the original input and manage it using IFS. For example:
Code:
IFS="%"
while read line_from_one line_from_two
do
  ((count++))
  echo "Line $count from file_one: $line_from_one"
  echo "Line $count from file_two: $line_from_two"
done < <(paste -d% file_one file_two)
You may want to save the original (default) IFS in another variable, then restore it after the "while read" job is finished (for further use in the subsequent code).
 
Old 05-31-2010, 06:30 AM   #7
yathin
LQ Newbie
 
Registered: May 2010
Location: India-Bangalore
Posts: 2

Original Poster
Rep: Reputation: 0
Yes,I have understood the words, thanks for the advice. Surely I will work hard here for the knowledge.
 
  


Reply

Tags
loops



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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash scripting problem with nested if statements in while loop error xskycamefalling Programming 4 05-11-2009 03:14 PM
Easy Bash Loop Scope Issue? scsi.king Programming 7 04-25-2007 05:41 AM
Nested Bash Script downbound010 Programming 1 12-10-2005 02:37 PM
bash script loop bong.mau Programming 6 09-14-2005 07:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:54 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
Open Source Consulting | Domain Registration