-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathexit_code.rs
More file actions
33 lines (30 loc) · 1.16 KB
/
exit_code.rs
File metadata and controls
33 lines (30 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use rexpect::error::Error;
use rexpect::process::wait;
use rexpect::spawn;
use std::time;
/// The following code emits:
/// cat exited with code 0, all good!
/// cat exited with code 1
/// Output (stdout and stderr): cat: /this/does/not/exist: No such file or directory
fn main() -> Result<(), Error> {
let p = spawn("cat /etc/passwd", Some(time::Duration::from_secs(2)))?;
match p.process.wait() {
Ok(wait::WaitStatus::Exited(_, 0)) => println!("cat exited with code 0, all good!"),
_ => println!("cat exited with code >0, or it was killed"),
}
let mut p = spawn(
"cat /this/does/not/exist",
Some(time::Duration::from_secs(2)),
)?;
match p.process.wait() {
Ok(wait::WaitStatus::Exited(_, 0)) => println!("cat succeeded"),
Ok(wait::WaitStatus::Exited(_, c)) => {
println!("Cat failed with exit code {c}");
println!("Output (stdout and stderr): {}", p.exp_eof()?);
}
// for other possible return types of wait()
// see here: https://tailhook.github.io/rotor/nix/sys/wait/enum.WaitStatus.html
_ => println!("cat was probably killed"),
}
Ok(())
}