如何获取db2cmd运行的DB2命令的输出?

时间:2018-05-22 06:59:49

标签: go cmd db2

我写了一个Go cli程序并使用Windows cmd运行它。 然后执行以下命令:

db2cmd -c DB2 RESTORE DATABASE DMSCNDB FROM "C:/DB" TAKEN AT 20180522033009 ON C: INTO DMSCNDB  WITHOUT PROMPTING

以下是我使用的Go代码:

cmd := exec.Command("db2cmd", "-c", arg)
buf, err := cmd.Output()
if err != nil {
    log.Fatalf("Failed restoring backup with error: %s\r\n", err)
} else {
    log.Printf("Successfully restored backup with command output: %s\r\n", buf)
}

db2cmd生成的命令提示符外,其他所有内容都被罚款。有没有办法得到它的输出?如何通过db2cmd运行DB2命令的输出?

谢谢!

1 个答案:

答案 0 :(得分:0)

我在这里找到了答案: https://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/com.ibm.db2.luw.admin.cmd.doc/doc/r0002036.html

所以,只需使用-i参数,如下所示:

cmd := exec.Command("db2cmd", "-i", arg)
buf, err := cmd.Output()
if err != nil {
    log.Fatalf("Failed restoring backup with error:\r\n%s\r\n", err)
} else {
    log.Printf("Successfully ran command with output:\r\n%s\r\n", buf)
}