I want to be able to use Rust to spawn a child shell, then repeatedly pass it arbitrary commands and process their outputs. I have found plenty of examples online showing me how to pass a single command and receive its single output, but I can't seem to be able to do it repeatedly.
For instance, the following code hangs on the line after the comment. (I imagine maybe read_to_string()
is blocking until it receives stdout from the child process, but if so I don't understand why that output isn't forthcoming..)
let mut child_shell = match Command::new("/bin/bash")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
{
Err(why) => panic!("couldn't spawn child_shell: {}", Error::description(&why)),
Ok(process) => process,
};
loop {
{
match child_shell.stdin.as_mut().unwrap().write("ls".as_bytes()) {
Err(why) => panic!(
"couldn't send command to child shell: {}",
Error::description(&why)
),
Ok(_) => println!("sent command to child shell"),
}
}
{
let mut s = String::new();
// ↓ hangs on this line ↓
match child_shell.stdout.as_mut().unwrap().read_to_string(&mut s) {
Err(why) => panic!("couldn't read bash stdout: {}", Error::description(&why)),
Ok(_) => print!("bash responded with:
{}", s),
}
}
}
I'm a beginner in Rust and I think the problem is my limited understanding of the borrow-checker/referencing rules, since the above runs fine (for a single iteration) if I remove the loop instruction from the code and change the references to the innards of the std::process::Child
struct to immutable; for instance from this:
child_shell.stdin.as_mut().unwrap().write("ls".as_bytes())
to this:
child_shell.stdin.unwrap().write("ls".as_bytes())
Obviously, repeatedly running ls
isn't my ultimate goal, and I know that I could just write a shell script and then have Rust repeatedly run it - but (apart from the goal of just learning more about Rust!) this is something I need to be able to do, at least in principle, for a more complicated project (which I'm happy to go into if it might prove relevant to any solutions, but it's probably way, way outside the scope of this question!)
Finally, if it turns out that it isn't possible to use a child shell in this way, I'd nevertheless like to learn how to repeatedly/continuously pipe to and from a spawned process running some other arbitrary command, as I wasn't able to find any info in the Rust documentation, tutorials or on Stack Overflow.
See Question&Answers more detail:
os