简体   繁体   中英

How to directly invoke the system shell in Go (golang)?

As per the golang documentation, go does not make a call to the system's shell when you are using exec.Command().

From the golang.org documentation on the "os/exec" package:

Unlike the "system" library call from C and other languages, the os/exec package intentionally does not invoke the system shell and does not expand any glob patterns or handle other expansions, pipelines, or redirections typically done by shells.

This presents a problem. Because of this design choice you cannot use piping when executing a command. Therefore the following code does not execute as desired.

package main

import (
        "fmt"
        "os/exec"
)

func main() {
        exec.Command("echo", "Hello", ">>", "~/thing").Run()
        cmdOut, _ := exec.Command("cat", "~/thing").Output()

        fmt.Println(cmdOut)
}

Instead of printing out the contents of a file that should contain the word 'Hello,' it instead prints out a blank newline. I have tried directly invoking bash like this:

package main

import (
        "fmt"
        "os/exec"
)

func main() {
        exec.Command("bash", "-c", "echo", "Hello", ">>", "~/thing").Run()
        cmdOut, _ := exec.Command("cat", "~/thing").Output()

        fmt.Println(cmdOut)
}


This, however, produces the same result as the original code. How can I directly invoke the system shell when using golang?

The second argument should be one string. In shell command you need to pass it as one string too. Also ~ is interpreted by bash. You can safely assume that sh exists. Bash shell is not a must.

package main                                                                                                                                                              

import (                                                                                                                                                                  
 "fmt"                                                                                                                                                                    
 "os/exec"                                                                                                                                                                
)                                                                                                                                                                         

func main() {                                                                                                                                                             
 exec.Command("sh", "-c", "echo Hello >> ~/thing").Run()                                                                                                                  
 cmdOut, _ := exec.Command("sh", "-c", "cat ~/thing").Output()                                                                                                            
 fmt.Println(cmdOut)                                                                                                                                                      
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM