Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使用Golang运行Grep命令时退出状态2_Bash_Shell_Go - Fatal编程技术网

Bash 使用Golang运行Grep命令时退出状态2

Bash 使用Golang运行Grep命令时退出状态2,bash,shell,go,Bash,Shell,Go,我使用Golang编写了一个简单的脚本来grep一个带有一些参数的日志文件。这是我的shell命令 grep CRON var/log/sys | tail-5 | grep“cd/home/raka/repo&&git status” 我想使用os/exec包在Golang中运行上面的命令。这是我的密码 var ( reader io.Reader out []byte err error commandName string = "grep" ) args :=

我使用Golang编写了一个简单的脚本来grep一个带有一些参数的日志文件。这是我的shell命令

grep CRON var/log/sys | tail-5 | grep“cd/home/raka/repo&&git status”

我想使用
os/exec
包在Golang中运行上面的命令。这是我的密码

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,上述截取操作不起作用。 你们中有人有解决这个问题的办法吗?非常感谢。

管道(
|
)由shell程序(如bash)实现。如果要使用它们,应执行shell,并传递包含管道程序调用的命令:

exec.Command("/bin/sh", "-c",
    "grep CRON var/log/sys | tail -5 | grep \"cd /home/raka/repo && git status\"")