How to convert Os.File output into string instead of printing to shell. I am using github.com/kr/pty library to execute commands.
How can convert the output to sting instead of printing to console.
f belongs to Os.File in below example. How to convert it to string.
package main
import (
"github.com/kr/pty"
"io"
"os"
"os/exec"
)
func run_command(cmd string){
c := exec.Command("/bin/sh", "-c", cmd)
f, err := pty.Start(c)
if err != nil {
panic(err)
}
io.Copy(os.Stdout, f)
}
func main() {
run_command("ls -lrt");
}
kr/ptywrites to the terminal, that's actually the whole point of it. If you just want the program's stdout, you should useos/execdirectly. It even has an example to show you how, you just need tostring(out)on the resulting[]byte.kr/ptywrites to the console, so there's nothing to read back), you can always use ioutil.ReadAll on the*os.File. But again: the fileptyopens is a terminal file descriptor, it just displays the contents, you don't be able to read them back.