Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exec with double quoted argument

Tags:

windows

go

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!

like image 949
user232343 Avatar asked Mar 10 '15 01:03

user232343


1 Answers

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.)

like image 132
Harry Johnston Avatar answered Oct 22 '22 08:10

Harry Johnston