Questions tagged [stdin]

Standard input (stdin, file descriptor 0) is the input stream to a program.

3861 questions
1748
votes
25 answers

How do I read from stdin?

How do I read from stdin? Some code golf challenges require using stdin for input.
tehryan
  • 24,405
  • 11
  • 28
  • 17
341
votes
12 answers

How do I pass a string into subprocess.Popen (using the stdin argument)?

If I do the following: import subprocess from cStringIO import StringIO subprocess.Popen(['grep','f'],stdout=subprocess.PIPE,stdin=StringIO('one\ntwo\nthree\nfour\nfive\nsix\n')).communicate()[0] I get: Traceback (most recent call last): File…
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
324
votes
10 answers

Best practices with STDIN in Ruby?

I want to deal with the command line input in Ruby: > cat input.txt | myprog.rb > myprog.rb < input.txt > myprog.rb arg1 arg2 arg3 ... What is the best way to do it? In particular I want to deal with blank STDIN, and I hope for an elegant…
griflet
  • 3,257
  • 3
  • 17
  • 4
323
votes
22 answers

How to read from a file or standard input in Bash

The following Perl script (my.pl) can read from either the file in the command line arguments or from standard input (STDIN): while (<>) { print($_); } perl my.pl will read from standard input, while perl my.pl a.txt will read from a.txt. This…
Dagang
  • 24,586
  • 26
  • 88
  • 133
304
votes
11 answers

Confused about stdin, stdout and stderr?

I am rather confused with the purpose of these three files. If my understanding is correct, stdin is the file in which a program writes into its requests to run a task in the process, stdout is the file into which the kernel writes its output and…
Shouvik
  • 11,350
  • 16
  • 58
  • 89
262
votes
10 answers

How to read from stdin line by line in Node

I'm looking to process a text file with node using a command line call like: node app.js < input.txt Each line of the file needs to be processed individually, but once processed the input line can be forgotten. Using the on-data listener of the…
Matt R. Wilson
  • 7,268
  • 5
  • 32
  • 48
221
votes
6 answers

Send string to stdin

Is there a way to effectively do this in bash: /my/bash/script < echo 'This string will be sent to stdin.' I'm aware that I could pipe the output from the echo such as this: echo 'This string will be piped to stdin.' | /my/bash/script
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
206
votes
8 answers

How to open every file in a folder

I have a python script parse.py, which in the script open a file, say file1, and then do something maybe print out the total number of characters. filename = 'file1' f = open(filename, 'r') content = f.read() print filename, len(content) Right…
B.Mr.W.
  • 18,910
  • 35
  • 114
  • 178
194
votes
10 answers

How to trick an application into thinking its stdout is a terminal, not a pipe

I'm trying to do the opposite of "Detect if stdin is a terminal or pipe?". I'm running an application that's changing its output format because it detects a pipe on STDOUT, and I want it to think that it's an interactive terminal so that I get the…
Chris
159
votes
9 answers

How to pass the value of a variable to the standard input of a command?

I'm writing a shell script that should be somewhat secure, i.e., does not pass secure data through parameters of commands and preferably does not use temporary files. How can I pass a variable to the standard input of a command? Or, if it's not…
user283145
148
votes
8 answers

nodejs how to read keystrokes from stdin

Is it possible to listen for incoming keystrokes in a running nodejs script? If I use process.openStdin() and listen to its 'data' event then the input is buffered until the next newline, like so: // stdin_test.js var stdin =…
bantic
  • 4,886
  • 4
  • 29
  • 34
148
votes
3 answers

Compress files while reading data from STDIN

Is it possible to compress (create a compressed archive) data while reading from stdin on Linux?
Space
  • 7,049
  • 6
  • 49
  • 68
142
votes
6 answers

Detect if stdin is a terminal or pipe?

When I execute "python" from the terminal with no arguments it brings up the Python interactive shell. When I execute "cat | python" from the terminal it doesn't launch the interactive mode. Somehow, without getting any input, it has detected that…
Mike McQuaid
  • 9,615
  • 6
  • 33
  • 38
122
votes
14 answers

How to read a line from the console in C?

What is the simplest way to read a full line in a C console program The text entered might have a variable length and we can't make any assumption about its content.
pbreault
  • 14,176
  • 18
  • 45
  • 38
115
votes
5 answers

How to pipe input to a Bash while loop and preserve variables after loop ends

Bash allows to use: cat <(echo "$FILECONTENT") Bash also allow to use: while read i; do echo $i; done
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
1
2 3
99 100