1

使用exec.Command执行带管道的命令

 9 months ago
source link: https://zhangyiming748.github.io/post/golang_use_cmd/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用exec.Command执行带管道的命令

2023-11-07 Golang

使用 sh -c ""命令
exec.Command("bash", "-c", "ps aux|grep go")
这是推荐的做法。
如果输出不是很多,推荐使用github.com/go-cmd/cmd库来执行系统命令,如:

import "github.com/go-cmd/cmd"

c := cmd.NewCmd("bash", "-c", "ps aux|grep go")
<-c.Start()
fmt.Println(c.Status().Stdout)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK