Reading from stdin Instead of a File

  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    File Reading
AI Thread Summary
The discussion centers on reading input in a bash script, specifically whether it's possible to switch from reading a file to standard input by simply changing the filename variable. One participant suggests that instead of using "done < $filename," the loop should end with just "done." This allows for standard input to be provided directly when the script is run, while still permitting file input by redirecting with "< filename." The emphasis is on understanding that the "<" operator is used to make a file appear as standard input, rather than the other way around.
zeion
Messages
455
Reaction score
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.
 
Technology news on Phys.org


Hi zeion,
is this what you are looking for ?
 


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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top