This question already has an answer here:
- calling command with some arguments works but not with others bu wotrks from console 1 answer
- go os/exec command argument issues [duplicate] 1 answer
- How to create an os.exec Command struct from a string with spaces 2 answers
- Running Linux commands with multiple arguments 2 answers
- How to pass a flag to a command in go lang? 1 answer
I have a sample go program that is not executing a specific kind of syntax. The same command string works just fine when executed directly on bash. Probably GoLang methods add quotes or something that I am unable to figure out. Here is the sample program
func main() {
cmd:= "/usr/bin/vmtoolsd --cmd 'info-get guestinfo.ovfEnv'"
//cmd:= "/usr/bin/vmtoolsd --help"
words := strings.Fields(cmd)
fmt.Printf("
words are : %v
",words)
oscmd := &exec.Cmd{
Path: words[0],
Args: words[:],
}
stdoutStderr, err:=oscmd.CombinedOutput()
fmt.Printf("err:%v
",err)
fmt.Printf("output:
%v
",string(stdoutStderr))
fmt.Printf("execute again")
cmd2:="/usr/bin/vmtoolsd"
arg:="--cmd \"info-get guestinfo.ovfEnv\""
output:=exec.Command(cmd2,arg).Run()
if output!=nil {
fmt.Printf("
some error
")
}
if output!=nil {
fmt.Println(os.Stderr,output.Error())
}
//fmt.Printf("%v",cmd)
}
Program output
[root@localhost tmp]# ./main
words are : [/usr/bin/vmtoolsd --cmd 'info-get guestinfo.ovfEnv']
err:exit status 1
output:
Unknown command
execute again
some error
&{0xc000052120} exit status 1
Note that if I simply change the command to /usr/bin/vmtoolsd --help
, it just works. I am certain it has got something to do with how the shell is interpreting the arguments in the failure case. Any tips will be appreciated.
</div>