Skip to content

Instantly share code, notes, and snippets.

@shovon
Created July 11, 2014 05:45
Show Gist options
  • Save shovon/513dd6cbf657301bfb20 to your computer and use it in GitHub Desktop.
Save shovon/513dd6cbf657301bfb20 to your computer and use it in GitHub Desktop.
An example to synchronously read from the command line.
var child_process = require('child_process');
var fs = require('fs');
process.stdout.pause();
var fd = child_process.execFile('/bin/sh', ["-c", "read LINE < /dev/tty; echo $LINE > stdout; echo 1 > done"]);
while (true) {
try {
if (fs.readFileSync('done', {encoding: 'utf8'}).trim() === '1') { break; }
} catch (e) {}
}
console.log(fs.readFileSync('stdout', {encoding: 'utf8'}).trim());
fs.unlinkSync('stdout');
fs.unlinkSync('done');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment