rust-shell-script / rust_cmd_lib

Common rust command-line macros and utilities, to write shell-script like tasks in a clean, natural and rusty way
https://docs.rs/cmd_lib/
Apache License 2.0
1.05k stars 37 forks source link

How would I pipe commands with no hardcoding of command strings? #32

Closed redragonx closed 3 years ago

redragonx commented 3 years ago

Hi, I would like to know how you can combine commands without hardcoding the actual cmd in the macro?

Not sure how escaping works in these cases.

Rough Example:

        let post_cmd  = "grep 'blah' | another_cmd"
        let output_raw = self.someVarEval();

        let data = run_fun!(echo "$output_raw" | $post_cmd );

When I run the above in full code. I get the following output error:


No such file or directory (os error 2)
rust-shell-script commented 3 years ago

You can construct command string in your way and pass it to bash:

let cmd_str = ....;
let data = run_fun!(bash -c $cmd_str)?;

It is not encouraged to do things like this, since it could introduce command injection and also you need to make the correct quotes. But anyway you are already doing things like that, so take your own risk.