Skip to content

Instantly share code, notes, and snippets.

@popomore
Created May 28, 2013 16:29
Show Gist options
  • Save popomore/5664045 to your computer and use it in GitHub Desktop.
Save popomore/5664045 to your computer and use it in GitHub Desktop.
shell redirect in nodejs
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
function run(cmd) {
var output, out, index = cmd.indexOf('>');
if (index > -1) {
output = cmd.substring(index + 1).replace(/(^\s+|\s+$)/g, '');
cmd = cmd.substring(0, index).split(/\s+/);
} else {
cmd = cmd.split(/\s+/);
}
if (output) {
out = fs.openSync(path.resolve(output), 'a');
}
spawn(cmd[0], cmd.slice(1), {stdio: [
process.stdin,
out ? out : process.stdout,
process.stderr
]});
}
run('echo hello > a.html');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment