4

I'm writing a small program with an interpreter, I would like to pipe any command that is not recognized by my shell to bash, and print the output as if written in a normal terminal.

func RunExtern(c *shell.Cmd) (string, os.Error) {   
    cmd := exec.Command(c.Cmd(), c.Args()...)
    out, err := cmd.Output()

    return string(out), err
}

this is what I've written so far, but it only executes a program with its args, I would like to send the whole line to bash and get the output, any idea how to do so ?

1 Answer 1

5

For example, to list directory entries in columns,

package main

import (
    "exec"
    "fmt"
    "os"
)

func BashExec(argv []string) (string, os.Error) {
    cmdarg := ""
    for _, arg := range argv {
        cmdarg += `"` + arg + `" `
    }
    cmd := exec.Command("bash", "-c", cmdarg)
    out, err := cmd.Output()
    return string(out), err
}

func main() {
    out, err := BashExec([]string{`ls`, `-C`})
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(out)
}
Sign up to request clarification or add additional context in comments.

1 Comment

the -c !! I tried running bash only, should've manned it. Thank's a lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.