/    Sign up×
Community /Pin to ProfileBookmark

how to capture output from interactive program

Hi All

I have this tool which has an interactive shell (like mysql client)
I need to feed it a couple commands and then It will produce output (many lines of text)
How would I capture its output?

thanks a lot


to post a comment

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 03.2012 — Usually you can redirect command line output to a file with the ">" operator, e.g.:
</i>php my_script.php arg1 arg2 &gt; results.txt
Copy linkTweet thisAlerts:
@jeanlucaauthorFeb 03.2012 — No thats not what I mean.I think you can compare what I need to do with using mysql client. To get data out of this tool you need to open it, feed it a SELECT statement and capture the output.

I noticed that proc_open can do bi-directional stuff. Here is my test script I made using the manual from proc_open

$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to

$cwd = './src';
$env = array();

$process = proc_open('./stockfish', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {

fwrite($pipes[0], "position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1ngo depth 20");

echo stream_get_contents($pipes[1]);

$return_value = proc_close($process);

echo "command returned $return_valuen";

But it doesn't print all the output, I think it print only the last line.

UPDATE: it seems that the program ignores the second argument given ("go depth 20"). So, the line printed by strem_get_contents is the line I need!

UPDATE2: the second argument is not ignored but I get the same result as when I would only provide "go" instead of "go depth 20".


Help @jeanluca spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 12.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ

legal: ({
terms: of use,
privacy: policy
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
recent_tips: (
tipper: @bahaedd,
tipped: article
amount: 1000 SATS,

tipper: @Balmasexy,
tipped: article
amount: 1000 SATS,

tipper: @mbsaad,
tipped: article
amount: 1000 SATS,