go语言的exec.Command方法执行失败

huangapple go评论99阅读模式
英文:

The go language exec.Command method execute failed

问题

这个命令执行失败了,并且没有log.txt存在。

为什么?

英文:
package main

import (
    "os/exec"
    "fmt"
)

func main(){
    cmd := exec.Command("find", "/home/d101270c/cbe", "-name","*.log", ">", "log.txt")
    fmt.Println(cmd)
    err := cmd.Run()
    if err != nil {
        fmt.Printf("%s\n", err)
        return
    }
}

Run result:

<1067 linux20 [d101270c] :/home/d101270c>uname -a
Linux linux20 2.6.16.60-0.21-smp #1 SMP Tue May 6 12:41:02 UTC 2008 x86_64 x86_64 x86_64 GNU/Linux
<1068 linux20 [d101270c] :/home/d101270c>tgo
&{/usr/bin/find [find /home/d101270c/cbe -name *.log > log.txt] []  <nil> <nil> <nil> [] <nil> <nil> <nil> <nil> false [] [] [] [] <nil>}
exit status 1

This command execute failed, and no log.txt exists.

Why?

答案1

得分: 3

你正在传递find的参数,比如>',它无法理解,但是shell可以。这对我有效:

(07:28) jnml@tsat:~/src/tmp/SO/11623232$ cat main.go 
package main

import (
	"fmt"
	"os"
	"os/exec"
)

func main() {
	//cmd := exec.Command("sh", "-c", "find /home/d101270c/cbe -name \\*.log > log.txt")
	cmd := exec.Command("sh", "-c", "find "+os.Getenv("HOME")+"/src -name \\*.go > go.txt")
	fmt.Println(cmd)
	err := cmd.Run()
	if err != nil {
		fmt.Printf("%s\n", err)
		return
	}
}
(07:29) jnml@tsat:~/src/tmp/SO/11623232$ go build main.go 
(07:29) jnml@tsat:~/src/tmp/SO/11623232$ ./main 
&{/bin/sh [sh -c find /home/jnml/src -name \*.go > go.txt] []  <nil> <nil> <nil> [] <nil> <nil> <nil> <nil> false [] [] [] [] <nil>}
(07:29) jnml@tsat:~/src/tmp/SO/11623232$ ls -l
celkem 1408
-rw-r--r-- 1 jnml jnml    7845 2012-07-24 07:29 go.txt
-rwxr-xr-x 1 jnml jnml 1428319 2012-07-24 07:29 main
-rw-r--r-- 1 jnml jnml     332 2012-07-24 07:28 main.go
(07:29) jnml@tsat:~/src/tmp/SO/11623232$ wc go.txt 
 131  131 7845 go.txt
(07:29) jnml@tsat:~/src/tmp/SO/11623232$ tail go.txt 
/home/jnml/src/github.com/nsf/gocode/config.go
/home/jnml/src/github.com/nsf/gocode/rpc.go
/home/jnml/src/github.com/nsf/gocode/_gccgo/package.go
/home/jnml/src/github.com/nsf/gocode/_goremote/goremote.go
/home/jnml/src/github.com/nsf/gocode/scope.go
/home/jnml/src/github.com/nsf/gocode/formatters.go
/home/jnml/src/github.com/nsf/gocode/autocompletefile.go
/home/jnml/src/github.com/nsf/gocode/decl.go
/home/jnml/src/tmp/go/main.go
/home/jnml/src/tmp/SO/11623232/main.go
(07:29) jnml@tsat:~/src/tmp/SO/11623232$
英文:

You're passing find arguments like `>', which it doesn't understand, but the shell does. This works for me:

(07:28) jnml@tsat:~/src/tmp/SO/11623232$ cat main.go 
package main

import (
	&quot;fmt&quot;
	&quot;os&quot;
	&quot;os/exec&quot;
)

func main() {
	//cmd := exec.Command(&quot;sh&quot;, &quot;-c&quot;, &quot;find /home/d101270c/cbe -name \\*.log &gt; log.txt&quot;)
	cmd := exec.Command(&quot;sh&quot;, &quot;-c&quot;, &quot;find &quot;+os.Getenv(&quot;HOME&quot;)+&quot;/src -name \\*.go &gt; go.txt&quot;)
	fmt.Println(cmd)
	err := cmd.Run()
	if err != nil {
		fmt.Printf(&quot;%s\n&quot;, err)
		return
	}
}
(07:29) jnml@tsat:~/src/tmp/SO/11623232$ go build main.go 
(07:29) jnml@tsat:~/src/tmp/SO/11623232$ ./main 
&amp;{/bin/sh [sh -c find /home/jnml/src -name \*.go &gt; go.txt] []  &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; [] &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; false [] [] [] [] &lt;nil&gt;}
(07:29) jnml@tsat:~/src/tmp/SO/11623232$ ls -l
celkem 1408
-rw-r--r-- 1 jnml jnml    7845 2012-07-24 07:29 go.txt
-rwxr-xr-x 1 jnml jnml 1428319 2012-07-24 07:29 main
-rw-r--r-- 1 jnml jnml     332 2012-07-24 07:28 main.go
(07:29) jnml@tsat:~/src/tmp/SO/11623232$ wc go.txt 
 131  131 7845 go.txt
(07:29) jnml@tsat:~/src/tmp/SO/11623232$ tail go.txt 
/home/jnml/src/github.com/nsf/gocode/config.go
/home/jnml/src/github.com/nsf/gocode/rpc.go
/home/jnml/src/github.com/nsf/gocode/_gccgo/package.go
/home/jnml/src/github.com/nsf/gocode/_goremote/goremote.go
/home/jnml/src/github.com/nsf/gocode/scope.go
/home/jnml/src/github.com/nsf/gocode/formatters.go
/home/jnml/src/github.com/nsf/gocode/autocompletefile.go
/home/jnml/src/github.com/nsf/gocode/decl.go
/home/jnml/src/tmp/go/main.go
/home/jnml/src/tmp/SO/11623232/main.go
(07:29) jnml@tsat:~/src/tmp/SO/11623232$ 

答案2

得分: 1

也许,在这里你甚至不需要使用shell。请参考http://golang.org/pkg/os/exec/#Cmd.Output。

Alex

英文:

Perhaps, you do not even need to use shell here. See http://golang.org/pkg/os/exec/#Cmd.Output.

Alex

huangapple
  • 本文由 发表于 2012年7月24日 10:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/11623232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定