dreadiscool
dreadiscool

Reputation: 1598

Spawn Bash Shell in Go

I'm writing a bot to play music in Go, and I'm having some trouble getting this bash shell to work

proc := exec.Command("/bin/bash")
stdin, errIn := proc.StdinPipe()
stdout, errOut := proc.StdoutPipe()
WriteAndWait(stdin, stdout, "cd /home/user/bot1337/")
WriteAndWait(stdin, stdout, "rm song.mp3")
WriteAndWait(stdin, stdout, "youtube-dl --extract-audio --audio-format mp3 " + Sanitize(command[1]))
WriteAndWait(stdin, stdout, "mv *.mp3 song.mp3")
WriteAndWait(stdin, stdout, "xmms2 play song.mp3")
WriteAndWait(stdin, stdout, "quit")

The WriteAndWait function

func WriteAndWait(stdout io.Writer, stdin io.Reader, command string) {
    stdout.Write([]byte(command + "; echo -e '\\x63\\x68\\x65\\x63\\x6b'\r\n"))
    buf := make([]byte, 256)
    for {
        fmt.Println("Reading...")
        rlen, _ := stdin.Read(buf)
        strin := string(buf[:rlen])
        fmt.Println(strin)
        if strings.Contains(strin, "check") {
            return
        }
    }
}

The process gets created, but it the program hangs on Read() - it can never read anything from stdin

Thanks in advance for your help!

Upvotes: 0

Views: 842

Answers (1)

creack
creack

Reputation: 121482

You forgot to start your process. exec.Command creates the process object, but does not start it.

Just put proc.Start() after your pipe init and it should do the trick.

Upvotes: 1

Related Questions