考虑以下C程序(test.c):
#include <stdio.h>
int main() {
printf("string out 1\n");
fprintf(stderr, "string err 1\n");
getchar();
printf("string out 2\n");
fprintf(stderr, "string err 2\n");
fclose(stdout);
}
Run Code Online (Sandbox Code Playgroud)
哪个应该打印一行到stdout,一行到stderr,然后等待用户输入,然后另一行到stdout,另一行到stderr.非常基本!在编译并在命令行上运行时,程序的输出完成(收到getchar()的用户输入):
$ ./test
string out 1
string err 1
string out 2
string err 2
Run Code Online (Sandbox Code Playgroud)
尝试使用带有以下代码的nodejs将此程序生成为子进程时:
var TEST_EXEC = './test';
var spawn = require('child_process').spawn;
var test = spawn(TEST_EXEC);
test.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
test.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
// Simulate entering data for getchar() after 1 second
setTimeout(function() { …Run Code Online (Sandbox Code Playgroud) 我有一个封闭的源程序,打印输出到标准输出.我需要解析输出.所以我使用dup2将输出重定向到fifo(我可以在父进程中读取,分叉和执行二进制文件),然后执行程序.问题是文件中的fprintf调用变为缓冲,因为它现在正在写入文件.
我试着在调用exec之前在stdout上用_IONBF调用setvbuf.但问题仍然存在.
为什么setvbuf在我的情况下没有帮助?
我如何强制输出刷新?
我想通过管道运行程序的输出,但是当它检测到stdout不是交互式shell 时,它显然表现不同.
如何在正常情况下通过管道欺骗它?
给定以下 Python 程序:
import sys
print("input")
while True:
pass
Run Code Online (Sandbox Code Playgroud)
和 C 中的等价物:
#include <stdio.h>
int main() {
printf("input\n");
while(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我使用管道cat传输程序时,默认情况下输出是缓冲的(而不是行缓冲的)。结果,我没有输出:
% python3 ./a.py | cat
(empty)
% ./a.out | cat
(empty)
Run Code Online (Sandbox Code Playgroud)
我可以stdbuf用来切换回行缓冲:
% stdbuf -oL ./a.out | cat
input
Run Code Online (Sandbox Code Playgroud)
但这不适用于 Python:
% stdbuf -oL python3 a.py | cat
(empty)
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?我知道存在python3 -u但我想要行缓冲,而不是“无缓冲”,我希望这个命令行也适用于其他语言。该命令unbuffer似乎也有效,但我想了解为什么stdbuf在这里不起作用。
假设我将 docker 容器作为守护进程运行:
docker run -d foo
Run Code Online (Sandbox Code Playgroud)
有没有办法写入该容器的标准输入?就像是:
docker exec -i foo echo 'hi'
Run Code Online (Sandbox Code Playgroud)
上次我检查-i和-d标志与docker run命令一起使用时是互斥的。
我想知道如何与我在命令行PHP脚本中运行的程序进行交互.场景是:
如何编写执行此操作的PHP脚本?我想我可能想用,proc_open()但我无法弄清楚如何.我认为它会是这样的,但它当然不起作用:
$descriptorspec = array(
0 => array('pipe', 'r'), //STDIN
1 => array('pipe', 'w'), //STDOUT
2 => array('pipe', 'r'), //STDERR
);
$process = proc_open('mycommand', $descriptorspec, $pipes, null, null);
if (is_resource($process)) {
// Get output until first question is asked
while ($buffer = fgets($pipes[1])) {
echo $buffer;
}
if (strpos($buffer, 'STEP 1:') !== false) {
fwrite($pipes[0], "My first answer\n"); //enter the answer
} else {
die('Unexpected last line before question');
}
// Get …Run Code Online (Sandbox Code Playgroud) 举个例子,我想用密码登录mysql.我知道我可以使用-pmypass,但我想学习如何在bash中重定向stdin.所以我的测试是
mysql -u limited_user -p <text to redirect into stdin when it prompts for my pass>
Run Code Online (Sandbox Code Playgroud)
我之前看过<filename但我不想将数据存储在文件中.我试过&和 - 但没有运气.我不确定是什么& - 做什么.
默认情况下,某些程序根据它们写入的流的类型格式化其输出.例如,输出ls和ls > file看起来不同.我想知道一个程序是如何实现的.另外,有没有一种方法可以欺骗这些程序,就好像输出流是一个实际上是文件的终端(特别是当它们没有任何影响输出格式的选项时)?
是否可以使用 Java 从 Python 获取控制台输出?以下是此类输出的示例:
Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2+2
4
>>>
Run Code Online (Sandbox Code Playgroud)
现在,主要目标是通过使用 Java 调用 Python 解释器来获得上述输出。这是我的尝试:
//...
//Irrelevant code omitted
ProcessBuilder processBuilder = new ProcessBuilder("cmd");
processBuilder.redirectErrorStream(true);
processBuilder.start();
processBuilder.command("python2");
Process pythonProcess = processBuilder.start();
OutputStream outputStream = pythonProcess.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(outputStream);
osw.write("2+2\r\nquit()\r\n");
osw.flush();
osw.close();
InputStream inputStream = pythonProcess.getInputStream();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while( (line=bufferedReader.readLine())!=null) …Run Code Online (Sandbox Code Playgroud) 在线编译器这是我的网站,用户可以在其中运行控制台程序。
目前,用户在运行程序之前必须输入程序输入。我正在尝试为该程序构建实时用户输入(希望提供与他们在笔记本电脑上运行程序相同的体验)。
在实现这一目标的研究中,我遇到了一种使用websocket流式传输 stdout 和 stdin 的解决方案。
我的实现
# coding: utf-8
import subprocess
import thread
from tornado.websocket import WebSocketHandler
from nbstreamreader import NonBlockingStreamReader as NBSR
class WSHandler(WebSocketHandler):
def open(self):
self.write_message("connected")
self.app = subprocess.Popen(['sh', 'app/shell.sh'], stdout=subprocess.PIPE, stdin=subprocess.PIPE,
shell=False)
self.nbsr = NBSR(self.app.stdout)
thread.start_new_thread(self.soutput, ())
def on_message(self, incoming):
self.app.stdin.write(incoming)
def on_close(self):
self.write_message("disconnected")
def soutput(self):
while True:
output = self.nbsr.readline(0.1)
# 0.1 secs to let the shell output the result
if not output:
print 'No more data'
break
self.write_message(output)
Run Code Online (Sandbox Code Playgroud)
nbstreamreader.py …
我做了一个包装的Minecraft与围棋服务器控制台。它使用 os/exec 运行服务器和 process.StdoutPipe() 以获取子进程的实时输出。
但是,由于某种原因,我无法让它显示颜色。当我直接从终端运行服务器时,颜色会起作用,但是当我从 Go 运行它时它根本不起作用。
更重要的是,当我从 Go 运行它时,日志文件上也没有颜色代码。但是直接从终端运行它,日志文件完全被颜色代码弄乱了。我真的不明白这是怎么发生的。
有没有什么办法解决这一问题?
谢谢!