3 ms·
I wouldn't use it as an interactive prompt personally - it's a bit too verbose - though I did know someone in college who did that :) Calling shell commands is
by brundolf 6y ago
I wouldn't use it as an interactive prompt personally - it's a bit too verbose - though I did know someone in college who did that :)
Calling shell commands is reasonably straightforward:
const { exec} = require('child_process')
const child = exec('ls', ['-lh', '/usr'])
console.log('error', child.error)
console.log('stdout ', child.stdout)
console.log('stderr ', child.stderr)
However, you can also get cool packages like this to use instead: https://github.com/shelljs/shelljs https://github.com/shelljs/shelljs
I will admit though that in most of the cases where I've used it I didn't really do any shelling-out, so I can't speak much to how that experience scales in practice. Mostly I was either 1) doing self-contained logic with files, web requests, etc, or 2) delegating out to JS build tools (which even when they have a CLI, almost always expose a nice JS API too). Ymmv, etc.
- mekster 6y agoIt's far easier to do it on perl. $output = `ls -lh /usr`; I don't think I can remember all the boilerplate of using the wrong language for the job if I only happen to write that once a month or less. Even PHP is easy. $output = shell_exec('ls -lh /usr');