rust - 如何在作为子进程运行的 shell 中执行命令?
问题描述
从如何在 Rust 中调用系统命令并捕获其输出的答案?,我可以执行一个命令,该命令本身会产生一个 shell 来执行命令。
use std::process::Command;
Command::new("sh")
.spawn()
.expect("sh command failed to start");
是否可以在这个新获得的 shell 中执行来自 Rust 的命令?
解决方案
let mut child = Command::new("sh").stdin(Stdio::piped())
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
child.stdin
.as_mut()
.ok_or("Child process stdin has not been captured!")?
.write_all(b"something...")?;
let output = child.wait_with_output()?;
来源:Rust Cookbook中的外部命令。
推荐阅读
- microservices - 微服务设计模式:微服务之间的通信
- fluentd - Fluentd 重新部署后未将日志推送到 Elastic Search
- database-design - 设计不同角色的登录系统
- reactjs - reactjs组件错误与卸载
- react-native - 检查 Flatlist 中每个项目的本地存储并根据结果 REACT NATIVE 显示图标
- flutter - 在 null 上调用了方法“[]”。接收者:空尝试调用:[](“图片”)
- groovy - 通过任务脚本从 nexus repo 中删除工件
- c - CS50 恢复分段错误 2021
- python - Python交互式shell在子进程中运行时不响应输入
- python - 当存在多个最大值时,TensorFlow 如何处理最大梯度?