doulan8846 2019-01-25 18:45
浏览 76
已采纳

执行psql命令时os / exec上的输出不完整

I'm currently using os/exec to run many psql commands in sequence.

My problem is that if there's an error during the execution of the script, the output doesn't show it.

Example: When executing the command file from the command line, the output is this:

SET
CREATE FUNCTION
psql:update-14.3.7.1/dml/----------------xxxxxxxx.sql:33: ERROR:  null value in column "tipoxxxxxxxx" violates not-null constraint
DETALHE:  Failing row contains (9, 0, null, null, null, null, AAAAAAAAA_BBBBBBBBBB_CCCCC_BANANA, null, null, null).
CONTEXTO:  SQL statement "insert INTO ----------------xxxxxxxx
        (id,versao,tipoxxxxxxxx,carrotdeapplefinal,carrotdetttttttt,ativa,funcionalidade,driver,alterar_na_xxxxxxxx)
        values
        (nextval('sq_----------------xxxxxxxx'),0,tipoaaaaaaaaa,carrotapple,carrottttttttt,alterarNaxxxxxxxx)"
PL/pgSQL function createorupdateaaaaaaaaa() line 22 at SQL statement
DROP FUNCTION

And executing the exact same file from the Go code, the output is:

SET
CREATE FUNCTION
DROP FUNCTION

My Go code for running commands(and showing the output) is:

    cmd := exec.Command(comando, argList...)
    var out bytes.Buffer
    var stderr bytes.Buffer
    cmd.Stdout = &out
    cmd.Stderr = &stderr
    err := cmd.Run()
    cmdS := out.String()
    fmt.Println("cmdS", cmdS)
    if err != nil {
        fmt.Println("XYZ")
        fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
    }

How to modify my code to show the other lines of the output?

  • 写回答

1条回答 默认 最新

  • dongshuo9350 2019-01-25 19:26
    关注

    Programs can use stderr and stdout however they choose. The presence of an execution error has no bearing on whether there was something written to stderr or not.

    Always check both streams when looking for all output from a program.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号