1

I would like to run a process and process the standard output of it while the process is running, but I'm not sure how to do it.

fn execute_command(&mut self){
        let (exec_cmd, cmd_args) = self.command_as_arguments.split_at(1);

        let mut cmd_output = Command::new(exec_cmd[0]);
        // std::process::Command = cmd_output

        for arg in cmd_args{
            cmd_output.arg(arg);
        }

//        let std_out_handle = cmd_output.stdout(Stdio::null());
        let std_out_handle = cmd_output.stdout(Stdio::piped());
//        let std_out_handle = cmd_output.stdout(Stdio::inherit());

        let ok_cprocess_handle = std_out_handle.spawn().unwrap();
        //process::Child = ok_cprocess_handle


        let mut ok_cprocess_handle_stdout = ok_cprocess_handle.stdout.unwrap();
//        println!("CHILD STDOUT={:?}",ok_cprocess_handle_stdout);

        let mut output_string = String::new();
        ok_cprocess_handle_stdout.read_to_string(&mut output_string);
        println!("CHILD STRING STDOUT={:?}",output_string);

        let mut output_vec = Vec::new();
        ok_cprocess_handle_stdout.read_to_end(&mut output_vec);
        println!("CHILD VEC STDOUT={:?}",output_vec);
    }

I edited and reduced the code, but when I run the program I get still an empty string if I try to read the output: ok_cprocess_handle_stdout.read_to_string(&mut output_string); println!("CHILD STRING STDOUT={:?}",output_string);

I am expecting read some output at that point, but what am I missing?

MKay
  • 21
  • 2
  • Idiomatic Rust style for variables is `snake_case`, `PascalCase` should only be used for types, and `camelCase` is not used in Rust. – Shepmaster Nov 20 '17 at 16:14
  • Does https://stackoverflow.com/q/31576555/155423, https://stackoverflow.com/q/21615188/155423, https://stackoverflow.com/q/43949612/155423, or https://stackoverflow.com/q/34611742/155423 answer your question? – Shepmaster Nov 20 '17 at 16:21
  • 1
    Possible duplicate of [How to send input to a program through stdin in Rust](https://stackoverflow.com/questions/21615188/how-to-send-input-to-a-program-through-stdin-in-rust) – Psychemaster Nov 20 '17 at 16:29
  • `cmd_output.stdout(null())` specifies that you are not interested in the process's stdout. As a result, `fine.stdout` is None; this is a feature. – user4815162342 Nov 20 '17 at 23:11

0 Answers0