Crates.io | run_script |
lib.rs | run_script |
version | 0.11.0 |
source | src |
created_at | 2017-11-04 13:55:22.736976 |
updated_at | 2024-10-03 11:52:59.263255 |
description | Run shell scripts in rust. |
homepage | http://github.com/sagiegurari/run_script |
repository | https://github.com/sagiegurari/run_script.git |
max_upload_size | |
id | 38117 |
size | 68,794 |
Run shell scripts in rust.
This library enables to invoke shell scripts based on their content.
While std::process::Command works great to execute standalone command, you need more manual code to take a script text and execute it.
For this purpose, this library was created.
Simply include the library and invoke the run/spawn function with the script text and run options:
use run_script::ScriptOptions;
fn main() {
let options = ScriptOptions::new();
let args = vec![];
// run the script and get the script execution output
let (code, output, error) = run_script::run(
r#"
echo "Directory Info:"
dir
"#,
&args,
&options,
)
.unwrap();
println!("Exit Code: {}", code);
println!("Output: {}", output);
println!("Error: {}", error);
// run the script and get a handle to the running child process
let child = run_script::spawn(
r#"
echo "Directory Info:"
dir
"#,
&args,
&options,
)
.unwrap();
let spawn_output = child.wait_with_output().unwrap();
println!("Success: {}", &spawn_output.status.success());
}
The library also provides the run_script!, spawn_script! and run_script_or_exit! macros for simpler usage.
use run_script::ScriptOptions;
fn main() {
// simple call to run script with only the script text
let (code, output, error) = run_script::run_script!(
r#"
echo "Test"
exit 0
"#
)
.unwrap();
println!("Exit Code: {}", code);
println!("Output: {}", output);
println!("Error: {}", error);
// run script invoked with the script text and options
let options = ScriptOptions::new();
let (code, output, error) = run_script::run_script!(
r#"
echo "Test"
exit 0
"#,
&options
)
.unwrap();
println!("Exit Code: {}", code);
println!("Output: {}", output);
println!("Error: {}", error);
// run script invoked with all arguments
let options = ScriptOptions::new();
let (code, output, error) = run_script::run_script!(
r#"
echo "Test"
exit 0
"#,
&vec!["ARG1".to_string(), "ARG2".to_string()],
&options
)
.unwrap();
println!("Exit Code: {}", code);
println!("Output: {}", output);
println!("Error: {}", error);
// spawn_script! works the same as run_script! but returns the child process handle
let child = run_script::spawn_script!(
r#"
echo "Test"
exit 0
"#
)
.unwrap();
println!("PID: {}", child.id());
}
In order to use this library, just add it as a dependency:
[dependencies]
run_script = "^0.11.0"
See full docs at: API Docs
See Changelog
Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.