medmond919 icon

Go Example #6

medmond919 | PRO | 03/28/18 11:44:35 AM UTC | 0 ⭐ | 331 👁️ | Never ⏰ | []
Go |

480 B

|

None

|

0 👍

/

0 👎

package main
 
import (
    "os"
    "os/exec"
  "io"
    "strings"
    "github.com/kr/pty"
)
 
// This will be the equivalent of (echo id; cat) | /bin/sh but nicer
// because it realocates a tty after.
 
func main() {
    buffer := strings.NewReader("id\n")
    reader := io.MultiReader(buffer, os.Stdin)
 
    cmd := exec.Command("/bin/sh")
    tty, _ := pty.Start(cmd)
    defer tty.Close()
 
    go func() {
        io.Copy(tty, reader)
    }()
    go func() {
        io.Copy(os.Stdout, tty)
    }()
 
    cmd.Wait()
}

Comments