Command line for entering pipes in a bash interpreter

I am writing a small program with an interpreter, I would like to transfer any command that is not recognized by my shell to bash and print the output as if it were written in a regular 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 wrote so far, but it only runs the program with its arguments, I would like to send a whole line to bash and get the output, any idea how to do this?

+4
source share
1 answer

For example, to list 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) } 
+5
source

Source: https://habr.com/ru/post/1387493/


All Articles