【发布时间】:2018-09-11 17:04:47
【问题描述】:
我想使用 golang 的 exec.Command() 运行以下 bash 命令
ls > sample.txt
为此我写了
_,err:=exec.Command("ls",">","sample.txt").Output()
但这似乎不起作用。我知道我可以使用
写入文件exec.Command().StdoutPipe()
但我想以这种方式单独写作。知道如何在 golang 中做到这一点吗?
【问题讨论】:
-
如果是bash命令,需要bash来执行。
ls不能使用>作为参数。 -
反正我可以在 go 中使用频道吗?
-
我不确定通道与执行命令有什么关系。您要么需要执行一个 shell,要么自己编写输出。