0

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

2
  • It's unclear from your description, what exactly you consider a response. Is it the standard output of a program? Or the standard error? Or both? Jul 13 at 6:09
  • If I'm not mistaken, > return_resp creates a file with that name, and you (vainly) try to echo a env variable with the same name.
    – NotX
    Jul 13 at 16:23

1 Answer 1

5

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
2
  • @shayam, just to emphasize this answer: with my_command > response_resp you have created the file response_resp, not a variable. Jul 13 at 12:32
  • This worked Thank you @pratheesh-pc Jul 13 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.