简体   繁体   English

Go(golang)中如何直接调用系统shell?

[英]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().根据 golang 文档,当您使用 exec.Command() 时,go 不会调用系统的 shell。

From the golang.org documentation on the "os/exec" package:来自关于“os/exec”包的 golang.org 文档:

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.与来自 C 和其他语言的“系统”库调用不同,os/exec 包有意不调用系统外壳,也不扩展任何全局模式或处理通常由外壳完成的其他扩展、管道或重定向。

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.它没有打印出应该包含单词“Hello”的文件内容,而是打印出一个空白的换行符。 I have tried directly invoking bash like this:我试过像这样直接调用 bash:

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?使用golang时如何直接调用系统shell?

The second argument should be one string.第二个参数应该是一个字符串。 In shell command you need to pass it as one string too.在 shell 命令中,您也需要将其作为一个字符串传递。 Also ~ is interpreted by bash.另外~由 bash 解释。 You can safely assume that sh exists.您可以放心地假设sh存在。 Bash shell is not a must. Bash shell 不是必须的。

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)                                                                                                                                                      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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