ds000001 2018-04-27 07:32
浏览 589

在Windows CMD中的GoLang中运行shell命令

I am trying to execute cloudfoundry CF Client from my GoLang. The cf login command usually will not get executed in my git-bash terminal / cygwin terminal. It gets executed properly only in Command-Prompt (CMD.exe). So I always do cf login command from my CMD.exe only.

Now i attempted to execute the command from GoLang and found that it is executing in cygwin / git-bash pattern and give me the below error as output

API endpoint: https://api.sys-pcf.cf.MYCOMPANY.com
Authenticating...
Credentials were rejected, please try again.
Authenticating...
Credentials were rejected, please try again.
Authenticating...
Credentials were rejected, please try again.

I get the same error when i execute cf login from cygwin. Below is my Go code

    cf :="cf"
    login := "login"
    sso := "--sso"
    apiFlag := "-a"

    command := exec.Command(cf,login,sso,apiFlag,foundationUrl)


    stdout, err := command.Output()

    if err != nil {
        fmt.Println("Error")
    }

    fmt.Print(string(stdout))

Please tell me how to execute these commands in windows-style or through command-prompt

The os.exec package link here tells that

Unlike the "system" library call from C and other languages, the os/exec package intentionally does not invoke the system shell

Note that the examples in this package assume a Unix system. They may not run on Windows, and they do not run in the Go Playground used by golang.org and godoc.org.

I think the exec package by itself doesn't support windows shell

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 连续两帧图像高速减法
    • ¥15 组策略中的计算机配置策略无法下发
    • ¥15 如何绘制动力学系统的相图
    • ¥15 对接wps接口实现获取元数据
    • ¥20 给自己本科IT专业毕业的妹m找个实习工作
    • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
    • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
    • ¥50 mac mini外接显示器 画质字体模糊
    • ¥15 TLS1.2协议通信解密
    • ¥40 图书信息管理系统程序编写