LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Nested while loops for bash novice (https://www.linuxquestions.org/questions/programming-9/nested-while-loops-for-bash-novice-4175439318/)

E-Rich6 11-29-2012 09:05 AM

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~$

linosaurusroot 11-29-2012 09:35 AM

linecount=1 needs to be INSIDE the outer loop.

E-Rich6 11-29-2012 09:47 AM

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

Thanks so much

E~$

David the H. 11-30-2012 10:04 AM

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!

E-Rich6 11-30-2012 10:53 AM

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.


All times are GMT -5. The time now is 11:51 PM.