LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-29-2012, 10:05 AM   #1
E-Rich6
LQ Newbie
 
Registered: Nov 2012
Distribution: Debian
Posts: 3

Rep: Reputation: Disabled
Nested while loops for bash novice


I sure this is an easy question for someone....

Below is a script that I wrote to just as a learning experience for me and something I can build upon to do some other things. I'm very new at this.

In this script I'm trying to write identical output to two different files, 1.txt and 2.txt.

Code:
#!/bin/bash

#Declare variables
linecount=1
filecount=1


#Outer loop counts files
while [ $filecount -lt 3 ]
do
        #Inner loop counts lines within files
        while [ $linecount -lt 11 ]

        do
        echo "This is line number" $linecount "in this file number " $filecount"." >> $filecount.txt
        let linecount+=1
        done


let filecount+=1


done

My inner loop is working for me on the first pass. I get the desired output to "1.txt". However I fail to execute the inner loop the second time for "2.txt".

Code:
ls *.txt
1.txt

cat 1.txt
This is line number 1 in this file number  1.
This is line number 2 in this file number  1.
This is line number 3 in this file number  1.
This is line number 4 in this file number  1.
This is line number 5 in this file number  1.
This is line number 6 in this file number  1.
This is line number 7 in this file number  1.
This is line number 8 in this file number  1.
This is line number 9 in this file number  1.
This is line number 10 in this file number  1.
I'm sure it's something very elementary that I'm screwing up here, but your time and assistance is much appreciated.

Thanks!
E~$
 
Old 11-29-2012, 10:35 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
linecount=1 needs to be INSIDE the outer loop.
 
Old 11-29-2012, 10:47 AM   #3
E-Rich6
LQ Newbie
 
Registered: Nov 2012
Distribution: Debian
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks!

Boy do I feel dumb. I knew it had to be something simple.

Thanks so much

E~$
 
Old 11-30-2012, 11:04 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
I have a few suggestions for you:

1) When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://wiki.bash-hackers.org/syntax/arith_expr

2) When quoting lines, best practice is generally to just quote the longest string possible. Variables will expand as long as they're in double-quotes, and indeed should always be protected by them to avoid word splitting problems.

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

3) Counting loops are generally better handled using the c-style for loop.

http://wiki.bash-hackers.org/syntax/ccmd/c_for

Code:
#!/bin/bash

#Declare variables
linecount=1
filecount=1

#Outer loop counts files
for (( filecount ; filecount < 3 ; filecount++ )); do

        #Inner loop counts lines within files
        for (( linecount=1 ; linecount < 11 ; linecount++ )); do

		echo "This is line number $linecount in this file number $filecount." >> "$filecount.txt"

	done

done
Other than that, keep up the good work!
 
Old 11-30-2012, 11:53 AM   #5
E-Rich6
LQ Newbie
 
Registered: Nov 2012
Distribution: Debian
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you much for the advice and the resources.

I was retooling my script to meeting these practices, manually changing rather than copy/paste to help make the syntax stick in my mind. Forgot the "< 3" in "filecount < 3" Ended up at 2559.txt before I figured out what I missed.

This amused me.
 
  


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
Perl - Nested Arrays and For loops Poetics Programming 4 12-18-2009 09:14 AM
Nested for loops and arrays in C iongrey Programming 10 04-24-2009 06:25 PM
Novice needs Bash help. manwithaplan Programming 12 02-25-2009 09:29 PM
Nested Loops In Shellscripting onacorpuscle Programming 1 11-09-2006 08:42 AM
Vectors not being reset in nested loops. C++ dangerboy Programming 8 09-22-2006 06:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:53 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