PxPlus User Forum

Twitter Twitter Twitter

Author Topic: Is it possible to capture return Val and std out from Linux commands.  (Read 1771 times)

Peter.Higgins

  • Diamond Member
  • *****
  • Posts: 124
    • View Profile
Hi All,
I am working with curl, and if it is possible, would like to obtain both the results of the curl operation, (return Value) and some --write-out  options such as connect time.  Will using read with stdio redirects do this?   
Have a terrific day
Peter

Allen Miglore

  • Silver Member
  • ***
  • Posts: 38
    • View Profile
    • UnForm
You can capture stdout from a pipeline by opening it with a pipe prefix (i.e. open(unt)"|curl ..."; read(lfo)response$; close(lfo).

Curl sends some things to stderr, so to capture those from a pipe you'd want to add 2>&1 to the end of the pipe command.  You should also be able to write output to files and then read the files when the command is done.  You might then just use invoke instead of a pipe.

Peter.Higgins

  • Diamond Member
  • *****
  • Posts: 124
    • View Profile
Alan,

Guess I am still confused if the curl exit code is also the std error?  Would the results be in left to right order if the 2>&1 is used, ie std out, std err.  I am not getting the results I would expect in the response by pestering gmail.com for http status infomation, there is no exit code.  This is what I used.
cmd$="|curl --silent --show-error --write-out ""\n%{http_code}\n"" http://www.gmail.com/  2>&1"

Allen Miglore

  • Silver Member
  • ***
  • Posts: 38
    • View Profile
    • UnForm
Curl will write the whole web server response body to stdout.  It will append the --write-out data to that.  So to get the status code you can just read everything to the end:

0010 LET CMD$="|curl --silent --write-out ""\n%{http_code}\n"" http://gmail.com0010:/"
0020 OPEN (UNT)CMD$
0030 WHILE 1
0040 READ (LFO,ERR=*BREAK)LINE$
0050 WEND
0060 CLOSE (LFO)
0070 PRINT LINE$

This produces "301", which is the redirect status code given by that url.