Results 1 to 7 of 7

Thread: Reading from a file using a for loop

  1. #1
    Join Date
    Feb 2013
    Beans
    27

    Reading from a file using a for loop

    When I want to read a file using a while loop all I do is just
    Code:
    while read line; echo $line done <$file
    But I am unable to do something similar like this using a for loop.
    Code:
    for ((count=1;count<=5;count++));read line; done <$file
    Could someone guide me in the direction for reading from a file in for loop? Thanks

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,915
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Reading from a file using a for loop

    Your syntax seems to work, though. However, since you will always have to check that "read" worked, the classic "while read" idiom with an explicit counter test in the loop would do the same thing.

    Edit: overlooked the missing "do"... but the advice still holds.
    Last edited by ofnuts; March 12th, 2013 at 12:13 PM.

  3. #3
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Reading from a file using a for loop

    Code:
    for ((count=1;count<=5;count++)) do read line; done <$file

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,367
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Reading from a file using a for loop

    Code:
    for ((count=1;count<=5;count++)); do read line; done < "$file"
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Reading from a file using a for loop

    If it is bash then semicolon after numeric for is optional. Actually, even spaces are optional, although I admit that this is bad practice:
    Code:
    for((count=1;count<=5;count++))do read line; done < "$file"
    Last edited by schragge; March 12th, 2013 at 03:05 PM.

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,367
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Reading from a file using a for loop

    o.O well well, I didn't know that
    but who thought allowing for a style different than all other flavors of do ... done loops is a great idea? Is there no value to consistency? o.O
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Reading from a file using a for loop

    Well, it's essentially a ksh compatibility feature. While pdksh/mksh/zsh wouldn't allow this, ksh93 and bash will. The reasoning goes like this:
    Traditional generic for loop command (for var in list) needs to be delimited either by newline or by semicolon for shell to know where the list ends (or if it is present at all). OTOH, the numeric for loop expression is already unambigously delimited by ((...)). Convenience wins over consistency.
    Disclaimer. I don't endorse such coding style by any means. It's really confusing, and not universally supported by all shells.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •