Note that there are some explanatory texts on larger screens.

plurals
  1. POUnix script command not capturing stdin when launched as external process
    text
    copied!<p>My situation is similar to <a href="https://stackoverflow.com/questions/220814/redirect-stdin-and-stdout-to-file?rq=1">Redirect Stdin and Stdout to File</a>. I want to run a program and capture the entire session (stdin and stdout) of running a program as though it had been run interactively from a terminal. </p> <p>From <a href="https://stackoverflow.com/questions/7564960/dump-terminal-session-to-file">Dump terminal session to file</a> I found out that the <code>script</code> command would work. Here is a simplified version of my program (written in Groovy) that does this.</p> <pre><code>import groovy.io.GroovyPrintWriter def OUTPUT_FILE = "output.txt" def sysout = new StringBuffer() def syserr = new StringBuffer() // On Linux (Ubuntu 12.04) - starts the bc command with script and capture session to OUTPUT_FILE Process proc = "script -q -f -c bc ${OUTPUT_FILE}".execute() // On OS X with a slightly different version of Script // Process proc = "script -q ${OUTPUT_FILE} bc".execute() proc.consumeProcessOutput(sysout, syserr) // Spawns separate threads that will consume the out to prevent overflow proc.withWriter { writer -&gt; // Writes the following commands to the spawned process def gWriter = new GroovyPrintWriter(writer) // Imagine that the user enters these lines gWriter.println "a = 5" gWriter.println "b = 4" gWriter.println "c = a * b" gWriter.println "c" gWriter.println "quit" } proc.waitForOrKill(20000) // Give it 20 seconds to complete or kill the process println 'Standard out captured from process:\n' + sysout println 'Standard err captured from process:\n' + syserr </code></pre> <p>The program I wrote works on the version of script available on OS X. On OS X, the entire session is captured in the sysout variable and also written to the OUTPUT_FILE correctly.</p> <p><strong>Here's what I expect to get</strong></p> <pre><code>bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. a = 5 b = 4 c = a * b c 20 quit </code></pre> <p>However, <strong>here is what I get</strong> on Linux (Ubuntu 12.04), it omits everything that was typed into STDIN and only captures STDOUT. Here is the content of sysout and OUTPUT_FILE</p> <pre><code>bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 20 </code></pre> <p>As you can see, the lines <code>a = 4, b = 5, etc</code> are missing.</p> <p>When I run the <code>script</code> command manually on Linux from the command line prompt (<code>script -q -f -c bc output.txt}</code> and enter the lines one by one, it works, i.e., all the lines are also written to OUTPUT_FILE.</p> <p>What additional steps do I need to do to get my program to work on Linux? I know that the program works on OS X so it is unlikely to be a problem with the program (or Groovy). I suspect that it must be something with how the Linux version of script behaves when launched as an external process.</p> <p>Any pointers would be appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload