|
9 | 9 | | [Generate random numbers with given distribution][ex-rand-dist] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] |
|
10 | 10 | | [Generate random values of a custom type][ex-rand-custom] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] |
|
11 | 11 | | [Run an external command and process stdout][ex-parse-subprocess-output] | [![regex-badge]][regex] | [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing] |
|
| 12 | +| [Run an external command passing it stdin and check for an error code][ex-parse-subprocess-input] | [![regex-badge]][regex] | [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing] | |
12 | 13 | | [Run piped external commands][ex-run-piped-external-commands] | [![std-badge]][std] | [![cat-os-badge]][cat-os] |
|
13 | 14 | | [Filter a log file by matching multiple regular expressions][ex-regex-filter-log] | [![regex-badge]][regex] | [![cat-text-processing-badge]][cat-text-processing]
|
14 | 15 | | [Declare lazily evaluated constant][ex-lazy-constant] | [![lazy_static-badge]][lazy_static] | [![cat-caching-badge]][cat-caching] [![cat-rust-patterns-badge]][cat-rust-patterns] |
|
@@ -328,14 +329,69 @@ fn run() -> Result<()> {
|
328 | 329 | # quick_main!(run);
|
329 | 330 | ```
|
330 | 331 |
|
| 332 | +[ex-parse-subprocess-input]: #ex-parse-subprocess-input |
| 333 | +<a name="ex-parse-subprocess-input"></a> |
| 334 | +## Run an external command passing it stdin and check for an error code |
| 335 | + |
| 336 | +[![std-badge]][std] [![cat-os-badge]][cat-os] |
| 337 | + |
| 338 | +Opens the `python` interpreter using an external [`Command`] and passes it a python statement |
| 339 | +for execution. [`Output`] of said statement is then parsed. |
| 340 | + |
| 341 | +```rust,no_run |
| 342 | +# #[macro_use] |
| 343 | +# extern crate error_chain; |
| 344 | +# |
| 345 | +use std::collections::HashSet; |
| 346 | +use std::io::Write; |
| 347 | +use std::process::{Command, Stdio}; |
| 348 | +# |
| 349 | +# error_chain!{ |
| 350 | +# errors { CmdError } |
| 351 | +# foreign_links { |
| 352 | +# Io(std::io::Error); |
| 353 | +# Utf8(std::string::FromUtf8Error); |
| 354 | +# } |
| 355 | +# } |
| 356 | +
|
| 357 | +fn run() -> Result<()> { |
| 358 | + let mut child = Command::new("python").stdin(Stdio::piped()) |
| 359 | + .stderr(Stdio::piped()) |
| 360 | + .stdout(Stdio::piped()) |
| 361 | + .spawn()?; |
| 362 | +
|
| 363 | + child.stdin |
| 364 | + .as_mut() |
| 365 | + .ok_or("Child process stdin has not been captured!")? |
| 366 | + .write_all(b"import this; copyright(); credits(); exit()")?; |
| 367 | +
|
| 368 | + let output = child.wait_with_output()?; |
| 369 | +
|
| 370 | + if output.status.success() { |
| 371 | + let raw_output = String::from_utf8(output.stdout)?; |
| 372 | + let words = raw_output.split_whitespace() |
| 373 | + .map(|s| s.to_lowercase()) |
| 374 | + .collect::<HashSet<_>>(); |
| 375 | + println!("Found {} unique words:", words.len()); |
| 376 | + println!("{:#?}", words); |
| 377 | + Ok(()) |
| 378 | + } else { |
| 379 | + let err = String::from_utf8(output.stderr)?; |
| 380 | + bail!("External command failed:\n {}", err) |
| 381 | + } |
| 382 | +} |
| 383 | +# |
| 384 | +# quick_main!(run); |
| 385 | +``` |
| 386 | + |
331 | 387 | [ex-run-piped-external-commands]: #ex-run-piped-external-commands
|
332 | 388 | <a name="ex-run-piped-external-commands"></a>
|
333 | 389 | ## Run piped external commands
|
334 | 390 |
|
335 | 391 | [![std-badge]][std] [![cat-os-badge]][cat-os]
|
336 | 392 |
|
337 | 393 | Shows up to the 10<sup>th</sup> biggest files and subdirectories in
|
338 |
| -the current working directory. It is equivalant to run: `du -ah . | |
| 394 | +the current working directory. It is equivalent to run: `du -ah . | |
339 | 395 | sort -hr | head -n 10`.
|
340 | 396 |
|
341 | 397 | It spawns Unix processes which are represented as [`Command`]s. In
|
|
0 commit comments