在node.js中,我想找到一种获取Unix终端命令输出的方法.有没有办法做到这一点?
function getCommandOutput(commandString){
// now how can I implement this function?
// getCommandOutput("ls") should print the terminal output of the shell command "ls"
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 nodejs 脚本运行 powershell 命令。我发现以下两篇文章向我展示了与我想要实现的目标类似的内容:\n使用 Nodejs 执行 Windows 命令\n从 Nodejs 执行 powershell 脚本
\n\n在按钮单击事件中,我尝试列出当前连接到系统的 USB 设备及其驱动器号(C、D、E 等)。如果我在 powershell 中单独运行该命令,它就可以工作(但我无法让它显示驱动器号)。但是,如果我将它作为脚本的一部分运行,它就不起作用。下面是我的代码:
\n\nif (process.platform === \'win32\' || process.platform === \'win64\') {\n exec("powershell.exe",["GET-WMIOBJECT win32_diskdrive | Where { $_.InterfaceType \xe2\x80\x93eq \'USB\' }"], function (err, stdout, stderr) {\n console.log(err);\n console.log(stdout);\n console.log(stderr);\n });\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我究竟做错了什么?
\n