LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
LinkBack Search this Thread
Old 12-23-2009, 10:24 PM   #1
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Rep: Reputation: 1
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
 
Old 12-23-2009, 10:28 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
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.

Last edited by ghostdog74; 12-23-2009 at 10:29 PM.
 
Old 12-24-2009, 10:55 AM   #3
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Original Poster
Rep: Reputation: 1
That didn't fix it. Same problem. Other ideas, anyone?

Devon

Last edited by DevonB; 12-28-2009 at 06:13 AM.
 
Old 12-28-2009, 06:13 AM   #4
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Original Poster
Rep: Reputation: 1
Never mind - It's <&1. That fixed it. Thanks!

Devon

Last edited by DevonB; 12-28-2009 at 08:29 AM.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
for loops, if statements PAvandal Linux - Newbie 13 07-20-2009 09:31 PM
Bash: Problems testing multiple statements with logical and (-a or &&) AncientPC Programming 4 04-14-2008 12:16 AM
C Loops and switch statements ripwheels8 Programming 4 11-13-2004 04:47 PM
PHP: egrep For Loops and If Statements jhrbek Programming 14 12-31-2003 12:26 PM


All times are GMT -5. The time now is 06:59 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration