问题描述
我使用Golang到grep一些参数的日志文件,写简单的脚本。这里是我的shell命令
I write simple script using Golang to grep a log file with some parameters. Here's my shell command
的grep科雷无功/日志/ SYS |尾-5 | grep的CD /家庭/拉嘎/回购和放大器;&安培; git的状态
我想在Golang使用 OS / EXEC
包上面运行的命令。这里是我的code狙击。
I want to run command above in Golang using os/exec
package. Here's my code sniped.
var (
reader io.Reader
out []byte
err error
commandName string = "grep"
)
args := []string{"CRON", "/var/log/syslog", "| tail -6", "| grep \"git status\""}
cmd := exec.Command(commandName, args...)
r, err = cmd.StdoutPipe()
err = cmd.Start()
out, err = ioutil.ReadAll(r)
err = cmd.Wait()
return strings.Split(string(out), "\n")```
目前,上述狙击不起作用,因为退出状态2
。
任你们/女士对这个问题的解决方案?太感谢你了。
Currently, the sniped above doesn't work, because of exit status 2
.Any of you guys/ladies have solution for this problem? thank you so much.
推荐答案
管( |
)由shell程序(如bash)的实现。如果你想使用它们,你应该执行shell传递命令包含管道程序调用:
Pipes (|
) are implemented by a shell program (like bash). If you want to use them you should execute shell passing a command containing piped program invocations:
exec.Command("/bin/sh", "-c",
"grep CRON var/log/sys | tail -5 | grep \"cd /home/raka/repo && git status\"")
这篇关于上运行使用Golang grep命令退出状态2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!