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?
 

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.

Similar threads

  • Programming and Computer Science
Replies
21
Views
512
  • Programming and Computer Science
Replies
1
Views
525
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
367
  • Programming and Computer Science
Replies
5
Views
363
  • Programming and Computer Science
Replies
6
Views
974
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
347
Back
Top