Golang应用执行Shell命令实战

 更新时间:2023年03月16日 09:47:20   作者:梦想画家  
本文主要介绍了Golang应用执行Shell命令实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

本文学习如何在Golang程序中执行Shell命令(如,ls,mkdir或grep),如何通过stdin和stdout传入I/O给正在运行的命令,同时管理长时间运行的命令。为了更好的理解,针对不同场景由浅入深提供几个示例进行说明,希望你能轻松理解。

exec包

使用官方os/exec包可以执行外部命令,当你执行shell命令,是需要在Go应用的外部运行代码,因此需要这些命令在子进程中运行。如下图所示:

在这里插入图片描述

每个命令在Go应用中作为子进程运行,并暴露stdin和stdout属性,我们可以使用它们读写进程数据。

运行基本Shell命令

运行简单命令并从它的输出中读取数据,通过创建*exec.Cmd实例实现。在下面示例中,使用ls列出当前目录下的文件,并从代码中打印其输出:

// create a new *Cmd instance
// here we pass the command as the first argument and the arguments to pass to the command as the
// remaining arguments in the function
cmd := exec.Command("ls", "./")

// The `Output` method executes the command and
// collects the output, returning its value
out, err := cmd.Output()
if err != nil {
  // if there was any error, print it here
  fmt.Println("could not run command: ", err)
}
// otherwise, print the output from running the command
fmt.Println("Output: ", string(out))

因为在当前目录下运行程序,因此输出项目根目录下文件:

> go run shellcommands/main.go

Output:  LICENSE
README.md
command.go

在这里插入图片描述

当运行exec,程序没有产生shell,而是直接运行给定命令,这意味着不会进行任何基于shell的处理,比如glob模式或扩展。举例,当运行ls ./*.md命令,并不会如我们在那个shell中运行命令一样输出readme.md

执行长时间运行命令

前面示例执行ls命令立刻返回结果,但当命令输出是连续的、或需要很长时间执行时会怎样呢?举例,运行ping命令,会周期性获得连续结果:

ping www.baidu.com 
PING www.a.shifen.com (36.152.44.95) 56(84) bytes of data.
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=1 ttl=128 time=11.1 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=2 ttl=128 time=58.8 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=3 ttl=128 time=28.2 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=4 ttl=128 time=11.1 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=5 ttl=128 time=11.5 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=6 ttl=128 time=53.6 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=7 ttl=128 time=10.2 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=8 ttl=128 time=10.4 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=9 ttl=128 time=15.8 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=10 ttl=128 time=16.5 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=11 ttl=128 time=10.9 ms
^C64 bytes from 36.152.44.95: icmp_seq=12 ttl=128 time=9.92 ms

如果尝试使用cmd.Output执行这类命令,则不会获得任何结果,因为Output方法等待命令执行结束,而ping无限期执行。因此需要自定义Stdout属性去读取连续输出:

cmd := exec.Command("ping", "google.com")

// pipe the commands output to the applications
// standard output
cmd.Stdout = os.Stdout

// Run still runs the command and waits for completion
// but the output is instantly piped to Stdout
if err := cmd.Run(); err != nil {
  fmt.Println("could not run command: ", err)
}

再次运行程序,输出结果于Shell中执行类似。

通过直接分配Stdout属性,我们可以在整个命令生命周期中捕获输出,并在接收到输出后立即对其进行处理。进程间io交互如下图所示:

在这里插入图片描述

自定义写输出

代替使用os.Stdout,还能通过实现io.Writer接口创建自定义写输出。

下面自定义代码在每个输出块前增加"received output: "前缀:

type customOutput struct{}

func (c customOutput) Write(p []byte) (int, error) {
	fmt.Println("received output: ", string(p))
	return len(p), nil
}

现在给命令输出赋值自定义写输出实例:

cmd.Stdout = customOutput{}

再次运行程序,会获得下面的输出。

使用Stdin给命令传递输入

前面示例没有给命令任何输入(或提供有限输入作为参数),大多数场景中通过Stdin流传递输入信息。典型的示例为grep命令,可以通过管道从一个命令串给另一个命令:

➜  ~ echo "1. pear\n2. grapes\n3. apple\n4. banana\n" | grep apple
3. apple

这里echo的输出作为stdin传给grep,输入一组水果,通过grep过滤仅输出apple.

*Cmd实例提供了输入流用于写入,下面实例使用它传递输入给grep子进程:

cmd := exec.Command("grep", "apple")

// Create a new pipe, which gives us a reader/writer pair
reader, writer := io.Pipe()
// assign the reader to Stdin for the command
cmd.Stdin = reader
// the output is printed to the console
cmd.Stdout = os.Stdout

go func() {
  defer writer.Close()
  // the writer is connected to the reader via the pipe
  // so all data written here is passed on to the commands
  // standard input
  writer.Write([]byte("1. pear\n"))
  writer.Write([]byte("2. grapes\n"))
  writer.Write([]byte("3. apple\n"))
  writer.Write([]byte("4. banana\n"))
}()

if err := cmd.Run(); err != nil {
  fmt.Println("could not run command: ", err)
}

输出结果:

3. apple

在这里插入图片描述

结束子进程

有一些命令无限期运行,需要能够显示信号去结束。举例,如果使用python3 -m http.server运行web服务或sleep 10000,则子进程会运行很长时间或无限期运行。

要停止进程,需要从应用中发送kill信号,可以通过给命令增加上下文实例实现。如果上下文取消,则命令也会终止执行:

ctx := context.Background()
// The context now times out after 1 second
// alternately, we can call `cancel()` to terminate immediately
ctx, _ = context.WithTimeout(ctx, 1*time.Second)

// sleep 10 second 
cmd := exec.CommandContext(ctx, "sleep", "10")

out, err := cmd.Output()
if err != nil {
  fmt.Println("could not run command: ", err)
}
fmt.Println("Output: ", string(out))

运行程序,1秒后输出结果:

could not run command:  signal: killed
Output:  

当需要在有限时间内运行命令或在一定时间内命令没有返回结果则执行备用逻辑。

总结

到目前为止,我们已经学习了多种执行unix shell命令和与之交互的方法。下面是使用os/exec包时需要注意的一些事情:

  • 当您希望执行通常不提供太多输出的简单命令时使用cmd.Output
  • 对于具有连续或长时间输出的函数应使用cmd.Run,并通过cmd.Stdout和cmd.Stdin与之交互
  • 在生产场景中,如果进程在给定时间内没有响应,须有超时并结束功能,可以使用取消上下文发送终止命令

到此这篇关于Golang应用执行Shell命令实战的文章就介绍到这了,更多相关Golang执行Shell命令内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 浅谈golang package中init方法的多处定义及运行顺序问题

    浅谈golang package中init方法的多处定义及运行顺序问题

    这篇文章主要介绍了浅谈golang package中init方法的多处定义及运行顺序问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-05-05
  • Golang Map类型的使用(增删查改)

    Golang Map类型的使用(增删查改)

    在Go中,map是哈希表的引用,是一种key-value数据结构,本文主要介绍了Golang Map类型的使用,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • Golang跨平台GUI框架Fyne的使用教程详解

    Golang跨平台GUI框架Fyne的使用教程详解

    Go 官方没有提供标准的 GUI 框架,在 Go 实现的几个 GUI 库中,Fyne 算是最出色的,它有着简洁的API、支持跨平台能力,且高度可扩展,下面我们就来看看它的具体使用吧
    2024-03-03
  • go中string、int、float相互转换的实现示例

    go中string、int、float相互转换的实现示例

    本文主要介绍了go中string、int、float相互转换的实现示例,文中根据实例编码详细介绍的十分详尽,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Go语言中struct的匿名属性特征实例分析

    Go语言中struct的匿名属性特征实例分析

    这篇文章主要介绍了Go语言中struct的匿名属性特征,实例分析了struct的匿名属性特征,对于深入学习Go语言程序设计具有一定参考借鉴价值,需要的朋友可以参考下
    2015-02-02
  • 详解Go-JWT-RESTful身份认证教程

    详解Go-JWT-RESTful身份认证教程

    这篇文章主要介绍了详解Go-JWT-RESTful身份认证教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • Go语言基础for循环语句的用法及示例详解

    Go语言基础for循环语句的用法及示例详解

    这篇文章主要为大家介绍了Go语言基础for循环语句的用法及示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-11-11
  • Golang中的错误处理深入分析

    Golang中的错误处理深入分析

    Go错误处理类似C语言,没有提供任何异常,以及类java语言使用的try/catch异常处理机制。Go异常处理仅简化为预定义的Error类型,Go没有提供异常处理机制,不能抛出类似许多其他语言的异常。相反,Golang集成了新的错误处理机制,如panic和recovery
    2023-01-01
  • Go设计模式之原型模式图文详解

    Go设计模式之原型模式图文详解

    原型模式是一种创建型设计模式, 使你能够复制已有对象, 而又无需使代码依赖它们所属的类,本文将通过图片和文字让大家可以详细的了解Go的原型模式,感兴趣的通过跟着小编一起来看看吧
    2023-07-07
  • Go存储基础使用direct io方法实例

    Go存储基础使用direct io方法实例

    这篇文章主要介绍了Go存储基础之如何使用direct io方法实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12

最新评论