cancel
Showing results for 
Search instead for 
Did you mean: 

Ignoring exit code from "system" on Windows

gyorokpeter
New Contributor
Is there a way to ignore the exit code (and therefore 'os error) and get the stdout+stderr of a process on Windows? On Linux the following works:
system"(program 2>&1; true)"
8 REPLIES 8

effbiae
New Contributor
maybe set errorlevel=0

jack@TP C:\Users\jack
> q                                                                                          
KDB+ 3.1 2014.08.22 Copyright (C) 1993-2014 Kx Systems                                       
w32/ 4()core 4095MB jack tp 192.168.2.12 NONEXPIRE                                           
                                                                                             
q)\dir ...
File Not Found
'os
q)\dir ... & set errorlevel=0                                                                
 Volume in drive C is Windows7_OS                                                            
 Volume Serial Number is 545A-BF90                                                           
                                                                                             
 Directory of C:\Users\jack\trade\...                                                        
                                                                                             
File Not Found                                                                               
q)                                                                                           
                                                                                             
                                                                                             
                                                                                             
                                                                                             

On 27 September 2015 at 17:53, Péter Györök <gyorokpeter1@gmail.com> wrote:
Is there a way to ignore the exit code (and therefore 'os error) and get the stdout+stderr of a process on Windows? On Linux the following works:
system"(program 2>&1; true)"

--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbplus+unsubscribe@googlegroups.com.
To post to this group, send email to personal-kdbplus@googlegroups.com.
Visit this group at http://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.

Is there a way to also capture the stderr from the command?

q)a:system"dir foo & set errorlevel=0 2>&1"
 Volume in drive D has no label.
 Volume Serial Number is E2F8-5938

 Directory of D:\Projects\q

File Not Found

q)a:system"dir foo 2>&1 & set errorlevel=0"
 Volume in drive D has no label.
 Volume Serial Number is E2F8-5938
 
 Directory of D:\Projects\q

File Not Found

I would expect the stderr to go into the variable instead of printed to the console.

q)a:system"dir util.q"
q)a
" Volume in drive D has no label."
" Volume Serial Number is E2F8-5938"
""
" Directory of D:\\Projects\\q"
""
"2015.08.23.  13:05               811 util.q"
"               1 File(s)            811 bytes"
"               0 Dir(s)  108\377448\377792\377576 bytes free"

2015. szeptember 27., vasárnap 11:11:53 UTC+2 időpontban effbiae a következőt írta:
maybe set errorlevel=0

jack@TP C:\Users\jack
> q                                                                                          
KDB+ 3.1 2014.08.22 Copyright (C) 1993-2014 Kx Systems                                       
w32/ 4()core 4095MB jack tp 192.168.2.12 NONEXPIRE                                           
                                                                                             
q)\dir ...
File Not Found
'os
q)\dir ... & set errorlevel=0                                                                
 Volume in drive C is Windows7_OS                                                            
 Volume Serial Number is 545A-BF90                                                           
                                                                                             
 Directory of C:\Users\jack\trade\...                                                        
                                                                                             
File Not Found                                                                               
q)                                                                                           
                                                                                             
                                                                                             
                                                                                             
                                                                                             

david_demner
New Contributor
To: =?utf-8?b?UMOpdGVyIEd5w7Zyw7Zr?= , Kdb+ Personal Developers
Can't you just error trap in kdb?

@[system;cmd;{}]

From: P=C3=A9ter Gy=C3=B6r=C3=B6k
Sent: Sunday, September 27, 2015 00:53
To: Kdb+ Personal Developers
Reply To: personal-kdbplus@googlegroups.com
Subject: [personal kdb+] Ignoring exit code from "system" on Windows

Is there a way to ignore the exit code (and therefore 'os error) and get the stdout+stderr of a process on Windows? On Linux the following works:
system"(program 2>&1; true)"

--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbplus+unsubscribe@googlegroups.com.
To post to this group, send email to personal-kdbplus@googlegroups.com.
Visit this group at http://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.

That swallows the error but also the useful output.

2015. szeptember 27., vasárnap 19:32:10 UTC+2 időpontban David Demner a következőt írta:
Can't you just error trap in kdb?

@[system;cmd;{}]

From: Péter Györök
Sent: Sunday, September 27, 2015 00:53
To: Kdb+ Personal Developers
Subject: [personal kdb+] Ignoring exit code from "system" on Windows

Is there a way to ignore the exit code (and therefore 'os error) and get the stdout+stderr of a process on Windows? On Linux the following works:
system"(program 2>&1; true)"

--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbpl...@googlegroups.com.
To post to this group, send email to personal...@googlegroups.com.
Visit this group at http://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.

ikorkhov
New Contributor
This works on Windows:
q)system"program 2>&1 || echo."

That still doesn't get the useful output:

q)system"dir foo 2>&1 || echo."
 Volume in drive D has no label.
 Volume Serial Number is E2F8-5938

 Directory of D:\Utility\q

File Not Found
," "

Only one space is returned, same as system"echo.".

2015. október 2., péntek 15:38:41 UTC+2 időpontban Igor Korkhov a következőt írta:
This works on Windows:
q)system"program 2>&1 || echo."

use cygwin's true.exe instead of cmd's echo. assume goodFile exists and badFile doesnt.

system "dir badFile 1>stdout 2>stderr || c:\\cygwin64\\bin\\true.exe"
system "dir goodFile 1>stdout 2>stderr || c:\\cygwin64\\bin\\true.exe"
read0 `:stdout
read0 `:stderr

Sorry, by bad. You should use parentheses:
system"(dir foo 2>&1 || echo.)"

HTH