@@ -15,13 +15,14 @@ set up the parent and child IO handles manually, like this in the
15
15
[ rust cookbook] ( https://rust-lang-nursery.github.io/rust-cookbook/os/external.html ) , which is often a tedious
16
16
work.
17
17
18
- A lot developers just choose shell(sh, bash, ...) scripts for such tasks, by using ` < ` to redirect input,
18
+ A lot of developers just choose shell(sh, bash, ...) scripts for such tasks, by using ` < ` to redirect input,
19
19
` > ` to redirect output and '|' to pipe outputs. In my experience, this is ** the only good parts** of shell script.
20
20
You can find all kinds of pitfalls and mysterious tricks to make other parts of shell script work. As the shell
21
21
scripts grow, they will ultimately be unmaintainable and no one wants to touch them any more.
22
22
23
23
This cmd_lib library is trying to provide the redirection and piping capabilities, and other facilities to make writing
24
- shell-script like tasks easily. For the [ rust cookbook examples] ( https://rust-lang-nursry.github.io/rust-cookbook/os/external.html ) ,
24
+ shell-script like tasks easily without launching any shell. For the
25
+ [ rust cookbook examples] ( https://rust-lang-nursry.github.io/rust-cookbook/os/external.html ) ,
25
26
they can usually be implemented as one line of rust macro with the help of this library, as in the
26
27
[ examples/rust_cookbook_external.rs] ( https://github.com/rust-shell-script/rust_cmd_lib/blob/master/examples/rust_cookbook_external.rs ) .
27
28
Since they are rust code, you can always rewrite them in rust natively in the future, if necessary without spawning external commands.
@@ -51,7 +52,8 @@ Since they are rust code, you can always rewrite them in rust natively in the fu
51
52
ls oops ;
52
53
cat oops ;
53
54
}. is_err () {
54
- eprintln! (" Run group command failed" );
55
+ // your error handling code
56
+ ...
55
57
}
56
58
```
57
59
@@ -75,9 +77,13 @@ like $a or ${a} in `run_cmd!` or `run_fun!` macros.
75
77
If they are part of string literals , you need to capture the declarations with `| a , b , ... | ` at the macros '
76
78
beginnings. e.g.
77
79
```rust
78
- let msg = "I love rust";
79
- run_cmd!(echo $msg)?;
80
- run_cmd!(|msg| echo "This is the message: $msg")?;
80
+ let dir = "my folder";
81
+ run_cmd!(|dir| echo "Creating $dir at /tmp")?;
82
+ run_cmd!(mkdir -p /tmp/$dir)?;
83
+
84
+ // or with group commands:
85
+ let dir = "my folder";
86
+ run_cmd!(echo "Creating $dir at /tmp"; mkdir -p /tmp/$dir)?;
81
87
```
82
88
You can consider "" as glue, so everything inside the quotes will be treated as a single atomic component.
83
89
0 commit comments