Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[BASH] How to change from reading from a file to reading from STDIN in while loop?

  1. Jun 4, 2012 #1
    Hi,

    So I can read from a file by doing:

    while read line; do
    ...
    done<$fileame

    Is it possible to change to reading from stdin instead of that file by changing the $filename variable instead of writing a new loop without a done< argument?

    Thanks.
     
  2. jcsd
  3. Jun 4, 2012 #2
    Re: [BASH] How to change from reading from a file to reading from STDIN in while loop

    Hi zeion,
    is this what you are looking for ?
     
  4. Jun 4, 2012 #3
    Re: [BASH] How to change from reading from a file to reading from STDIN in while loop

    Hhhhmmm....I think you should be going the other way around.

    I have not tested any of this and don't have time at the moment, but maybe you should test WITHOUT "done < $filename" in the first place...

    you see "<" was invented, precisely to make a file look like is coming from standard input...not to make standard input look like a file..

    ...in other words, what I am saying is that you finish your while loop with simply "done" and THEN, if you want to feed it standard input, well, you simply start typing AND if you want to feed it with an input file, well, you run your bash script and follow it with "< filename"

    would that work for you?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [BASH] How to change from reading from a file to reading from STDIN in while loop?
  1. Reading from file (Replies: 3)

Loading...