@无名啊,以Node.js 创建子进程为例
import * as child from 'child_process'
export async function execRequest(cmd: string, commands: string[]) {
return new Promise((resolve) => {
let spawnChild = child.spawn(cmd)
spawnChild.stdout.on('data', ch => {
console.log(ch.toString().trim())
})
spawnChild.stderr.on('data', ch => {
console.log(ch.toString().trim())
})
spawnChild.on('exit', code => {
console.log(code)
})
commands.forEach((command) => {
spawnChild.stdin.write(`${command}\n`)
})
spawnChild.stdin.end('\nexit\n')
})
}
await execRequest('bash',['alias python233=python2','python233 --version 2>&1'])
以上代码 大概意思是,输入bash,并开启交互式shell
第一次流写入alias python233=python2 并且回车
第二次流写入python233 --version 2>&1 并且回车
输出流提示
提示python233不存在
PS: python --version竟然输出到错误流 真离谱