[−][src]Function fake_tty::make_script_command
pub fn make_script_command(command: &str) -> String
Wraps the command in the script
command that can execute it
pretending to be a tty.
This can be executed in two ways:
- by passing it to
bash -c <command>
(you can use thebash_command
function for this) or - by running
bash
and piping the command to stdin, followed with the commandexit
Examples
Pass it to bash -c <command>
:
use std::process::{Command, Stdio}; use fake_tty::make_script_command; let script_command = make_script_command("ls"); let output = Command::new("bash") .args(&["-c", &script_command]) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .output().unwrap(); assert!(output.status.success());
Pipe it to bash
:
use std::process::{Command, Stdio}; use std::io::Write; use fake_tty::make_script_command; let mut script_command = make_script_command("ls"); script_command.push_str("\nexit"); let process = Command::new("bash") .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn() .unwrap(); let mut stdin = process.stdin.as_ref().unwrap(); stdin.write_all(script_command.as_bytes()).unwrap(); let output = process.wait_with_output().unwrap(); assert!(output.status.success());