Reading from stdin Instead of a File

  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    File Reading
Click For Summary
SUMMARY

The discussion focuses on reading input in a Bash script using standard input (stdin) instead of a file. The user initially attempts to read from a file using a while loop with a redirection operator. A participant suggests removing the redirection operator "done < $filename" and simply using "done" to allow for both stdin and file input. This approach enables users to type input directly or redirect a file when executing the script.

PREREQUISITES
  • Basic understanding of Bash scripting
  • Familiarity with input redirection in Unix-like systems
  • Knowledge of the while loop syntax in Bash
  • Experience with standard input and output concepts
NEXT STEPS
  • Explore Bash input redirection techniques
  • Learn about the "read" command in Bash scripting
  • Investigate the differences between stdin and file input handling
  • Practice writing Bash scripts that utilize both stdin and file inputs
USEFUL FOR

Anyone interested in Bash scripting, including developers, system administrators, and users looking to enhance their command-line input handling skills.

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?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
35
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K