stdin

Redirect stdin and stdout in Java

冷暖自知 提交于 2019-11-27 14:14:14
I'm trying to redirect stdin and stdout of a subprocess in java, eventually i'm going to have the output go to a JTextArea or something. Here is my current code, Process cmd = Runtime.getRuntime().exec("cmd.exe"); cmd.getOutputStream().write("echo Hello World".getBytes()); cmd.getOutputStream().flush(); byte[] buffer = new byte[1024]; cmd.getInputStream().read(buffer); String s = new String(buffer); System.out.println(s); The output looks like this: Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\(Current Directory)> I'm expecting to see

Reading line by line from STDIN

北慕城南 提交于 2019-11-27 13:24:53
问题 I want to do something like this: $ [mysql query that produces many lines] | php parse_STDIN.php In parse_STDIN.php file I want to be able to parse my data line by line from stdin. 回答1: use STDIN constant as file handler. while($f = fgets(STDIN)){ echo "line: $f"; } Note: fgets on STDIN reads the \n character. 回答2: You could also use a generator - if you don't know how large the STDIN is going to be. Requires PHP 5 >= 5.5.0, PHP 7 Something along the lines of: function stdin_stream() { while

java: how to both read and write to & from process thru pipe (stdin/stdout)

早过忘川 提交于 2019-11-27 13:23:04
(i'm new to java) I need to start a process and receive 2 or 3 handles: for STDIN, STDOUT, (and STDERR), so I can write input to the process and receive its output, the same way command line pipes behave (e.g. "grep") in Python this is acheived with the following code: from subprocess import Popen, PIPE p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE) (child_stdin, child_stdout) = (p.stdin, p.stdout) child_stdin.write('Yoram Opposum\n') child_stdin.flush() child_stdout.readlines() What's the Java equivalent?? I've tried so far Process p = Runtime.getRuntime().exec(cmd); BufferedReader inp =

getpasswd functionality in Go?

五迷三道 提交于 2019-11-27 11:33:27
Situation: I want to get a password entry from the stdin console - without echoing what the user types . Is there something comparable to getpasswd functionality in Go? What I tried: I tried using syscall.Read , but it echoes what is typed. you can do this by execing stty -echo to turn off echo and then stty echo after reading in the password to turn it back on gihanchanuka The following is one of best ways to get it done. First get terminal package by go get golang.org/x/crypto/ssh package main import ( "bufio" "fmt" "os" "strings" "syscall" "golang.org/x/crypto/ssh/terminal" ) func main() {

Standard input and output units in Fortran 90?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 11:10:51
问题 How can I read and write to the standard input, output and error streams stdin , stdout and stderr in Fortran? I've heard writing to stderr , for example, used to be write(5, fmt=...) , with 5 the unit for stderr , and I know the way to write to stdout is to use write(*, fmt=...) . How do I read and write to the standard input and output units with the ifort compiler? Compiler version: Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version 10.0 Build 20070426 Package ID: l

Reading console input in Kotlin

蓝咒 提交于 2019-11-27 10:19:19
问题 I am attempting to accept input from the console in Kotlin but it is difficult because I am not too sure about the syntax. I begin with the main fun main(args: Array<String>) { } WHAT should I enter after this? I am aware that the println() and readline() are involved but I do not know how to structure them. Objective: prompt user to enter a number, the number entered is multiplied by 6, program returns the result to the console display. 回答1: Here are A+B examples in Kotlin reading from stdin

Reading an integer from standard input

巧了我就是萌 提交于 2019-11-27 09:55:15
问题 How do I use the fmt.Scanf function in Go to get an integer input from the standard input? If this can't be done using fmt.Scanf , what's the best way to read a single integer? 回答1: http://golang.org/pkg/fmt/#Scanf All the included libraries in Go are well documented. That being said, I believe func main() { var i int _, err := fmt.Scanf("%d", &i) } does the trick 回答2: An alternative that can be a bit more concise is to just use fmt.Scan : package main import "fmt" func main() { var i int fmt

Programmatically read from STDIN or input file in Perl

天大地大妈咪最大 提交于 2019-11-27 09:39:26
问题 What is the slickest way to programatically read from stdin or an input file (if provided) in Perl? 回答1: while (<>) { print; } will read either from a file specified on the command line or from stdin if no file is given If you are required this loop construction in command line, then you may use -n option: $ perl -ne 'print;' Here you just put code between {} from first example into '' in second 回答2: This provides a named variable to work with: foreach my $line ( <STDIN> ) { chomp( $line );

How to send input to the console as if the user is typing?

别来无恙 提交于 2019-11-27 09:30:30
This is my problem. I have a program that has to run in a TTY, cygwin provides this TTY. When I redirect stdIn the program fails because it does not have a TTY. I cannot modify this program, and need some way of automating it. How can I grab the cmd.exe window and send it data and make it think the user is typing it? I'm using C#, I believe there is a way to do it with java.awt.Robot but I have to use C# for other reasons. This sounds like a task for SendKeys() . It's not C#, but VBScript, but nontheless - you asked for some way of automating it: Set Shell = CreateObject("WScript.Shell") Shell

Read from File, or STDIN

拟墨画扇 提交于 2019-11-27 09:29:31
问题 I've written a command line utility that uses getopt for parsing arguments given on the command line. I would also like to have a filename be an optional argument, such as it is in other utilities like grep, cut etc. So, I would like it to have the following usage tool -d character -f integer [filename] How can I implement the following? if a filename is given, read from the file. if a filename is not given, read from STDIN. 回答1: In the simplest terms: import sys # parse command line if file