如何在Go(golang)中直接调用系统外壳? [英] How to directly invoke the system shell in Go (golang)?

查看:96
本文介绍了如何在Go(golang)中直接调用系统外壳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据golang文档,当您使用exec.Command()时,go不会调用系统的外壳程序.

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

来自"os/exec"软件包中的golang.org文档:

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

与使用C和其他语言进行的系统"库调用不同,os/exec软件包有意不调用系统外壳程序,也不扩展任何glob模式或处理其他通常由外壳程序完成的扩展,管道或重定向.

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

与其打印出应该包含单词"Hello"的文件内容,不如输出空白的换行符.我已经尝试过像这样直接调用bash:

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


但是,这将产生与原始代码相同的结果.使用golang时如何直接调用系统外壳?

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

推荐答案

第二个参数应该是一个字符串.在shell命令中,您也需要将其作为一个字符串传递. bash也解释~.您可以放心地假设sh存在. Bash shell不是必须的.

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

这篇关于如何在Go(golang)中直接调用系统外壳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆