双重引用论证的高管 [英] exec with double quoted argument

查看:129
本文介绍了双重引用论证的高管的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆