28 releases

0.2.5 Jan 17, 2019
0.2.3 Nov 28, 2018
0.1.19 Jul 22, 2018
0.1.14 Mar 20, 2018
0.1.6 Nov 6, 2017

#3 in Command line utilities

Download history 1647/week @ 2018-11-08 939/week @ 2018-11-15 647/week @ 2018-11-22 1377/week @ 2018-11-29 983/week @ 2018-12-06 263/week @ 2018-12-13 301/week @ 2018-12-20 259/week @ 2018-12-27 778/week @ 2019-01-03 593/week @ 2019-01-10 1240/week @ 2019-01-17 785/week @ 2019-01-24 450/week @ 2019-01-31 982/week @ 2019-02-07 411/week @ 2019-02-14

3,468 downloads per month
Used in 4 crates

Apache-2.0

590KB
537 lines

run_script

crates.io Build Status Build status codecov
license Libraries.io for GitHub Documentation downloads
Built with cargo-make

Run shell scripts in rust.

Overview

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.

Usage

Simply include the library and invoke the run function with the script text and run options:

extern crate run_script;

use run_script::ScriptOptions;

fn main() {
    let mut options = ScriptOptions::new();
    options.runner = None; // The script runner, for example bash. By default for windows it's cmd.exe and for other systems it is sh.
    options.capture_output = true; // True to capture and return the output. False will print it to the parent process output.
    options.exit_on_error = false; // Adds set -e option (not available for windows)
    options.print_commands = false; // Adds set -x option (not available for windows)

    let args = vec![];

    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);
}

The library also provides the run_script! macro for simpler usage.

#[macro_use]
extern crate run_script;

use run_script::ScriptOptions;

fn main() {
    // simple call to run script with only the script text
    let (code, output, error) = run_script!(
        r#"
        echo "Test"
        exit 0
        "#
    ).unwrap();

    // run script invoked with the script text and options
    let options = ScriptOptions::new();
    let (code, output, error) = run_script!(
        r#"
        echo "Test"
        exit 0
        "#,
        &options
    ).unwrap();

    // run script invoked with all arguments
    let options = ScriptOptions::new();
    let (code, output, error) = run_script!(
        r#"
        echo "Test"
        exit 0
        "#,
        &vec!["ARG1".to_string(), "ARG2".to_string()],
        &options
    ).unwrap();
}

Installation

In order to use this library, just add it as a dependency:

[dependencies]
run_script = "*"

API Documentation

See full docs at: API Docs

Contributing

See contributing guide

Release History

Date Version Description
2019-01-17 v0.2.5 Maintenance
2018-03-20 v0.1.14 Fix permissions issue (#2)
2017-12-23 v0.1.10 New run_script! macro
2017-11-04 v0.1.1 Initial release.

License

Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.

Dependencies

~1MB