Reputation: 153
I've been trying to wrap my head around a few simple implementations of systems programming involving the ability to call Bash from C and Rust. I was curious if there was a way to modify the following statement, in Rust specifically, to allow me to get a return value of 4
from a Bash script that runs in the following manner:
let status = Command::new("pathtoscript").status().expect("failed to execute process");
Rust String stuff confuses me initially, but any combination of actions that leads to status granting me access to the value of 4
being returned to the parent process would be great. Thank you so much in advance for helping me. I have checked the Rust documentation but I haven't found anything for getting things BACK to the parent process, only to the child process.
I should say it goes without saying for my application writing to a file and reading from that file is not sufficient or secure enough.
Upvotes: 1
Views: 996
Reputation: 16283
If you need the exit code 4
use status.code()
:
use std::process::Command;
fn main() {
let status = Command::new("./script.sh")
.status()
.expect("failed to execute process");
println!("{}", status.code().unwrap()); // 4
}
My script.sh
file:
#!/bin/bash
# Will exit with status of last command.
# exit $?
# echo $?
# Will return 4 to shell.
exit 4
And see this too:
use std::process::Command;
let status = Command::new("mkdir")
.arg("projects")
.status()
.expect("failed to execute mkdir");
match status.code() {
Some(code) => println!("Exited with status code: {}", code),
None => println!("Process terminated by signal")
}
Upvotes: 2