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 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 Thanks! E~$ |
linecount=1 needs to be INSIDE the outer loop.
|
Thanks!
Boy do I feel dumb. I knew it had to be something simple.
Thanks so much E~$ |
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 |
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. |