Reading from stdin Instead of a File

In summary, the conversation discusses the possibility of changing the $filename variable in a while loop to read from standard input instead of a specific file. The person suggests testing the loop without the "done < $filename" argument and using the standard input by typing or using an input file by running a bash script followed by "< filename."
  • #1
zeion
466
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
  • #2


Hi zeion,
is this what you are looking for ?
 
  • #3


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?
 

FAQ: Reading from stdin Instead of a File

What is the difference between reading from stdin instead of a file?

Reading from stdin (standard input) means reading data directly from the user's keyboard or from another program's output. Reading from a file means reading data from a specific file on the computer.

Why would I want to read from stdin instead of a file?

Reading from stdin allows for more flexibility in terms of where the data is coming from. It also allows for real-time input from the user, whereas reading from a file requires pre-existing data.

How do I read from stdin in a programming language?

The specific syntax for reading from stdin will vary depending on the programming language. However, most languages have a built-in function or method for reading from stdin, such as input() in Python or scanf() in C. Consult the language's documentation for specific instructions.

Is it possible to read from stdin and a file at the same time?

Yes, it is possible to read from both stdin and a file simultaneously. This can be useful for combining user input with pre-existing data from a file.

What are some common use cases for reading from stdin?

Reading from stdin is commonly used in interactive programs, such as command line interfaces or chatbots. It is also useful for accepting user input in programming challenges or exercises. Additionally, some programs may use stdin for configuration or setup purposes.

Back
Top