LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH How while loops handle multiple if statements? (http://www.linuxquestions.org/questions/linux-newbie-8/bash-how-while-loops-handle-multiple-if-statements-777789/)

DevonB 12-23-2009 10:24 PM

BASH How while loops handle multiple if statements?
 
I'm having an issue with a bit of code -

Code:

while read my_var
  do
    let "COUNTER += 1"
    if [ $var = "0" ]
      then
        printf "Stuff"
      else
        printf "Other stuff"
    fi

    if [ $COUNTER = "30" ]
      then
        COUNTER=0
        read -s -p "Press Enter To Continue - "
    fi
  done < my_file.txt

I have a list of items in this file. I want to make sure it stops after reading in so many lines (in this case, 30) and make you press enter to continue. First off, the read line doens't work at all for some reason. Second off, every time it actually meets the condition of the second if statement, the while loop increments twice. Why?

So i'd have -

...28
29
30
32...

If the second if is remarked out, it works normally through the list. Why does the second if make the while operate twice? What should I use instead that would allow me to read in a whole line of a file as well?

Devon

ghostdog74 12-23-2009 10:28 PM

read from stdin.
Code:

...
read -s -p "Press Enter To Continue - " <&0
...

if you don't put &0, you are basically getting the next line into your loop.

DevonB 12-24-2009 10:55 AM

That didn't fix it. Same problem. Other ideas, anyone? :(

Devon

DevonB 12-28-2009 06:13 AM

Never mind - It's <&1. That fixed it. Thanks!

Devon


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