You're not actually doing anything with the output in that code, other than assigning it to a variable. hello is a Vec<u8> containing the output - you'll could either convert it to a string:
use std::process::Command;
use std::str;
fn main() {
let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(&["/C", "echo hello"])
.output()
.expect("failed to execute process")
} else {
Command::new("sh")
.arg("-c")
.arg("echo hello")
.output()
.expect("failed to execute process")
};
let hello = output.stdout;
let hello2 = str::from_utf8(&hello).unwrap();
println!("{}", hello2);
}
Or you could write it directly to the parent process' stdout:
use std::process::Command;
use std::io::prelude::*;
use std::io;
fn main() {
let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(&["/C", "echo hello"])
.output()
.expect("failed to execute process")
} else {
Command::new("sh")
.arg("-c")
.arg("echo hello")
.output()
.expect("failed to execute process")
};
io::stdout().write_all(&output.stdout).unwrap();
}
hmmm ok that seems to work, but what about if the Command::new("cmd") is meant to run args to export registry subkey? e.g. .args(&["/C", "reg export", r#"HKEY_CURRENT_USER\Software\\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles" "c:\testcode\test.reg"#])
I don't know just trying to hash up something quick that'll export registry, read the export find specific server details in it, modify it and then reimport when launchin exe.
Without Windows here, but it looks like "reg" and "export" should be two arguments, not one? That, or the whole command for /C could be required as a single argument.
So typically on Windows CMD, the following can be run which does the export
reg export "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles" "C:\2020\coding\test.reg"
Seeing that even in the command prompt (cmd) the registry path and the target path both require double quotes around them for it to work.
Also the following didn't work either: Command::new("cmd").args(&[r#"reg export "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles" "c:\2020\test.reg""#])
Thanks for your help anyway - very prompt responses - wasn't expecting any responses for at least a week lol
Why do you use cmd /C ...? In the example of echo hello, the cmd /C is needed because echo is not an executable named echo.exe, but a built-in functionality of cmd.exe. So cmd.exe has to be called with an argument telling it to execute the built-in function echo.
If you run another command using cmd /C, I think you need to have the argument as one long string, because cmd.exe itself splits the string at the whitespaces to supply individual arguments to the called command. So you could use
In this call, you have to separate your arguments by putting them in individual &strs in the args vector,
because there is no cmd.exe to do the "split at whitespace" step.