36 releases

✓ Uses Rust 2018 edition

new 0.6.2 Feb 17, 2020
0.6.0 Jan 18, 2020
0.4.0 Dec 24, 2019
0.3.2 Jul 23, 2019
0.1.6 Nov 6, 2017

#20 in Command-line interface

Download history 1909/week @ 2019-11-07 1469/week @ 2019-11-14 3963/week @ 2019-11-21 3592/week @ 2019-11-28 3205/week @ 2019-12-05 1709/week @ 2019-12-12 1369/week @ 2019-12-19 1811/week @ 2019-12-26 2729/week @ 2020-01-02 5476/week @ 2020-01-09 8117/week @ 2020-01-16 4387/week @ 2020-01-23 1657/week @ 2020-01-30 2240/week @ 2020-02-06 1345/week @ 2020-02-13

13,773 downloads per month
Used in 7 crates

Apache-2.0

1MB
796 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/spawn function with the script text and run options:

extern crate run_script;

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! and spawn_script! macros 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();

    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!(
        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!(
        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 = spawn_script!(
        r#"
         echo "Test"
         exit 0
         "#
    )
    .unwrap();

    println!("PID: {}", child.id());
}

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

See Changelog

License

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

Dependencies

~1MB