双重引用论证的高管 [英] exec with double quoted argument
问题描述
我想用 exec
包来执行 find
Windows命令,但是windows正在做一些奇怪的转义。>
我有类似的东西:
out,err:= exec.Command(find,`SomeText` ).Output()
但是这是抛出错误,因为Windows将它转换为
find / SomeText
有人知道为什么吗?我如何执行使用exec包在windows上找到
?
谢谢!
好的,这可能比您预想的要复杂一些,但 是一个解决方案:
package main
$(
fmt
os / exec
系统调用
)
func main(){
cmd:= exec.Command(`find`)
cmd.SysProcAttr =& syscall.SysProcAttr {}
cmd.SysProcAttr.CmdLine = `找到SomeTexttest.txt`
out,err:= cmd.Output()
fmt.Printf(%s \ n,out)
fmt.Printf(%v \ n,err)
}
不幸的是,尽管对此的支持已于2011年添加,但似乎未将其纳入文档呢。 (虽然也许我只是不知道在哪里看。)
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!
OK, it's a bit more complicated than you might have expected, but there is a solution:
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)
}
Unfortunately, although support for this was added in 2011, it doesn't appear to have made it into the documentation yet. (Although perhaps I just don't know where to look.)
这篇关于双重引用论证的高管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!