13 releases (4 stable)

2.1.0 Mar 28, 2023
2.0.2 Feb 23, 2023
1.1.0-beta.0 Feb 4, 2023
0.1.2-beta.0 Jan 30, 2023

#763 in Unix APIs

Download history 7/week @ 2024-07-03 84/week @ 2024-07-24 25/week @ 2024-07-31 1/week @ 2024-09-04 3/week @ 2024-09-11 12/week @ 2024-09-18 23/week @ 2024-09-25 11/week @ 2024-10-02 11/week @ 2024-10-09

57 downloads per month

MIT license

39KB
759 lines

rsbash

GitHub issues Cargo Downloads Cargo Latest version

About

rsbash - run bash commands from rust.

Our macros rash! and rashf! allow you to call out to a bash shell, just as you would typically from a terminal. Since this is accomplished by interacting with libc, these macros can only be used on unix-like platforms (Linux, macOS etc).

Documentation

Check out the docs - https://docs.rs/rsbash

License

MIT License - Copyright (c) 2023 Luke Elliot


lib.rs:

rsbash: run bash commands from rust.

Our macros rash! and rashf! allow you to call out to a bash shell, just as you would typically from a terminal. Since this is accomplished by interacting with libc, these macros can only be used on unix-like platforms (Linux, macOS etc).

Motivation

Making a shell command with the native std::process::Command builder is quite involved.

Suppose you wanted to write "Hello world!" to stdout.

 use std::io::Write;
 use std::process::Command;

 let command = Command::new("echo")
               .arg("Hello world!")
               .output()
               .expect("Uh oh, couldn't say hello!");
 std::io::stdout().write_all(&command.stdout).unwrap();

 assert_eq!(std::str::from_utf8(&command.stdout).unwrap(), "Hello world!\n");

Now suppose you wanted to pipe the output to a second command, and then write the result to stdout:

use std::process::{Command, Stdio};
use std::io::Write;

let echo = Command::new("echo")
           .arg("Hello world!")
		   .stdout(Stdio::piped())
		   .spawn()
		   .expect("Uh oh, couldn't say hello!");
					   
let grep = Command::new("grep")
           .arg("Hello")
           .stdin(Stdio::from(echo.stdout.unwrap()))
           .output()
           .expect("Uh oh, couldn't grep for Hello!");
    
std::io::stdout().write_all(&grep.stdout).unwrap();

assert_eq!(std::str::from_utf8(&grep.stdout).unwrap(), "Hello world!\n");

With rash! the same command is as simple as:

 use rsbash::rash;

 let (ret_val, stdout, stderr) = rash!("echo 'Hello world!' | grep 'Hello'").unwrap();
 assert_eq!(stdout, "Hello world!\n");

See the rash! and rashf! macros, and the RashError for more information.

Dependencies

~2–10MB
~131K SLoC