doucanrui1735 2019-01-11 20:30
浏览 84

Golang Exec.Command和Bash问题[重复]

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>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
    • ¥15 cs loadimage运行不了,easyx也下了,没有用
    • ¥15 r包runway详细安装教程
    • ¥15 Html中读取Json文件中数据并制作表格
    • ¥15 谁有RH342练习环境
    • ¥15 STM32F407 DMA中断问题
    • ¥15 uniapp连接阿里云无法发布消息和订阅
    • ¥25 麦当劳点餐系统代码纠错
    • ¥15 轮班监督委员会问题。
    • ¥20 关于变压器的具体案例分析