如何捕获从”go run main.go”命令的响应?

huangapple go评论84阅读模式
英文:

How to capture the response from go run main.go

问题

我有一个名为main.go的Go脚本,我从shell脚本中调用它。main.go在成功时返回以下响应。我希望在调用main.go的同一个shell脚本中将返回的响应捕获到一个变量中。

> 运行go run main.go的响应

  No content to read in :/labs/text/11/004/11-004_M0_1_arm_1.d78d350eef91f1e72d801e064a088cf80d3c840a-0023.txt


2023/07/13 04:46:52 /app/lab-processor/src/main.go:201 SLOW SQL >= 200ms
[1267.568ms] [rows:1] SELECT * FROM "pipeline1_2"."p_doc_pp_dates" WHERE filename LIKE '%11-004_M0_1_arm_1.d78d350eef91f1e72d801e064a088cf80d3c840a%' AND pagenumber = 38 ORDER BY begin desc LIMIT 1

第一个响应是当运行第一个文件时,没有内容可读取,第二个响应是当文件中有内容并且加载到表中时。

> 我尝试过类似以下的方法

go run main.go > return_resp
echo $return_resp 

但是这并没有太大帮助。

英文:

I have go script main.go which I am calling it from shell script. main.go returns the response as below on success. I am looking to capture the return response in a variable from the same shell script I am calling main.go

> Response from running go run main.go

  No content to read in :/labs/text/11/004/11-004_M0_1_arm_1.d78d350eef91f1e72d801e064a088cf80d3c840a-0023.txt


2023/07/13 04:46:52 /app/lab-processor/src/main.go:201 SLOW SQL >= 200ms
[1267.568ms] [rows:1] SELECT * FROM "pipeline1_2"."p_doc_pp_dates" WHERE filename LIKE '%11-004_M0_1_arm_1.d78d350eef91f1e72d801e064a088cf80d3c840a%' AND pagenumber = 38 ORDER BY begin desc LIMIT 1

First response is when ran first file where there is no content to read and second response in when there is something in the file and it loaded in the table

> I have tried something like

go run main.go > return_resp
echo $return_resp 

however it was not much of help

答案1

得分: 5

你可以尝试以下代码:

return_resp=$(go run main.go)
echo $return_resp

或者

go run main.go > output.txt 2>&1
cat output.txt

关于 "2>&1" 的含义,你可以参考这个链接:What does "2>&1" mean?

英文:

You can try these

return_resp=$(go run main.go)
echo $return_resp

or

go run main.go > output.txt 2>&1
cat output.txt

huangapple
  • 本文由 发表于 2023年7月13日 12:57:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定