使用双引号执行命令

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

exec with double quoted argument

问题

我想使用exec包执行find Windows命令,但是Windows系统会进行一些奇怪的转义。

我有以下代码:

out, err := exec.Command("find", `"SomeText"`).Output()

但是这会抛出错误,因为Windows会将其转换为

find /SomeText"

有人知道为什么吗?我该如何使用exec包在Windows上执行find命令?

谢谢!

英文:

I want to execute find Windows command using exec package, but windows is doing some weird escaping.

I have something like:

out, err := exec.Command("find", `"SomeText"`).Output()

but this is throwing error because Windows is converting this to

find /SomeText"

Does anyone know why? How I can execute find on windows using exec package?

Thanks!

答案1

得分: 7

好的,以下是翻译好的内容:

package main

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

func main() {
    cmd := exec.Command(`find`)
    cmd.SysProcAttr = &syscall.SysProcAttr{}
    cmd.SysProcAttr.CmdLine = `find "SomeText" test.txt`
    out, err := cmd.Output()
    fmt.Printf("%s\n", out)
    fmt.Printf("%v\n", err)
}

很不幸,尽管在2011年添加了对此的支持,但它似乎还没有出现在文档中。(也许我只是不知道在哪里找。)


<details>
<summary>英文:</summary>

OK, it&#39;s a bit more complicated than you might have expected, but there *is* a solution:

    package main
    
    import (
        &quot;fmt&quot;
        &quot;os/exec&quot;
        &quot;syscall&quot;
    )
    
    func main() {
        cmd := exec.Command(`find`)
        cmd.SysProcAttr = &amp;syscall.SysProcAttr{}
        cmd.SysProcAttr.CmdLine = `find &quot;SomeText&quot; test.txt`
        out, err := cmd.Output()
        fmt.Printf(&quot;%s\n&quot;, out)
        fmt.Printf(&quot;%v\n&quot;, err)
    }

Unfortunately, [although support for this was added in 2011][1], it doesn&#39;t appear to have made it into [the documentation][2] yet.  (Although perhaps I just don&#39;t know where to look.)


  [1]: https://github.com/golang/go/issues/1849
  [2]: http://golang.org/pkg/os/exec/

</details>



# 答案2
**得分**: 0

FYI,运行:

```go
package main

import (
	"fmt"
	"os/exec"
)

func main() {
	cmd := exec.Command("find", `"SomeText"`)
	fmt.Printf("Path: %q, args[1]: %q\n", cmd.Path, cmd.Args[1])
}

在Unix系统上运行结果为:

Path: "/usr/bin/find", args[1]: "\"SomeText\""

在编译为Windows并在Win7上运行的结果为:

Path: "C:\\Windows\\system32\\find.exe", args[1]: "\"SomeText\""

两者看起来都是正确的。

在Windows交叉编译中添加out, err := cmd.Output(),对于fmt.Printf("%#v\n%v\n", err, err),输出如下:

&exec.ExitError{ProcessState:(*os.ProcessState)(0xc0820046a0)}
exit status 1

但我想这只是因为find未能找到任何内容。

英文:

FYI, running:

package main

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

func main() {
	cmd := exec.Command(&quot;find&quot;, `&quot;SomeText&quot;`)
	fmt.Printf(&quot;Path: %q, args[1]: %q\n&quot;, cmd.Path, cmd.Args[1])
}

<kbd>playground</kbd>

On unix gives:

Path: &quot;/usr/bin/find&quot;, args[1]: &quot;\&quot;SomeText\&quot;&quot;

And cross compiled to Windows and run on Win7 gives:

Path: &quot;C:\\Windows\\system32\\find.exe&quot;, args[1]: &quot;\&quot;SomeText\&quot;&quot;

Both look correct to me.

Adding out, err := cmd.Output() to the Windows cross-compile gives the following for fmt.Printf(&quot;%#v\%v\n&quot;, err, err):

&amp;exec.ExitError{ProcessState:(*os.ProcessState)(0xc0820046a0)}
exit status 1

But I imagine that's just because find fails to find anything.

huangapple
  • 本文由 发表于 2015年3月10日 09:29:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/28954729.html
匿名

发表评论

匿名网友

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

确定