PxPlus User Forum

Main Board => Discussions => Programming => Topic started by: Peter.Higgins on July 18, 2018, 04:53:55 PM

Title: Is it possible to capture return Val and std out from Linux commands.
Post by: Peter.Higgins on July 18, 2018, 04:53:55 PM
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
Title: Re: Is it possible to capture return Val and std out from Linux commands.
Post by: Allen Miglore on July 18, 2018, 08:09:03 PM
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.
Title: Re: Is it possible to capture return Val and std out from Linux commands.
Post by: Peter.Higgins on July 19, 2018, 02:04:54 PM
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"
Title: Re: Is it possible to capture return Val and std out from Linux commands.
Post by: Allen Miglore on July 19, 2018, 03:40:52 PM
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.