Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.