View Full Version : How to redirect output of command called from OS_Shell()?
EmbeddedMan
07-03-2018, 18:09
I have a simple TB script which is a console mode script.
In it, I us OS_Shell() to call another program. That other program outputs text to the console.
Unfortunately, the output of the other program messes up the nice pretty output of my TB script in it's console.
Is there a way to call another program from within TB such that the other program's console output does NOT get added to the TB console output?
Thanks!
*Brian
ErosOlmi
07-03-2018, 19:18
Try redirect your shell command to NUL
uses "OS"
uses "console"
OS_Shell("CMD.EXE /C DIR > NUL")
WaitKey
EmbeddedMan
07-03-2018, 19:20
Unfortunately, that appeared to have no effect. :-(
Still get the extra output in my TB console.
*Brian
ErosOlmi
07-03-2018, 19:37
Redirecting into a file?
If possible send me e little example showing what you are doing to better understand.
support@thinbasic.com
Petr Schreiber
07-03-2018, 19:54
Hello,
I think you have to redirect both stderr and stdout.
This could do it:
uses "OS", "Console"
OS_Shell("CMD.EXE /C dir > NUL 2>&1")
WaitKey
The 2>&1 looks strange, but it basically redirects stderr to stdout and that stdout you already send to NUL :)
Let us know!
Petr
EmbeddedMan
07-03-2018, 22:58
Hmm. That's a great suggestion, unfortunately it doesn't seem to work either.
Without TB at all, if I just do this from the command line:
C:\Users\bschmalz>C:\PEMicro\cyclone_stmicro\sap_launch port=USB1 IMAGE=1
Success
C:\Users\bschmalz>
It's the "Success" that I don't want. That's the output of the sap_launch.exe program.
If I do:
C:\Users\bschmalz>C:\PEMicro\cyclone_stmicro\sap_launch port=USB1 IMAGE=1 > NULL 2>&1
C:\Users\bschmalz>
Then the result of the sap_launch program is not printed to the console. That worked, right?
Not so fast. When I do this from within TB:
OutputText("Cycle " + Using$("####",x) + " reprogramming = ")
' Send command to Cyclone to reprogram motherboard
lProgResult = OS_Shell("C:\PEMicro\cyclone_stmicro\sap_launch port=USB1 IMAGE=1 > NULL 2>&1", %OS_WNDSTYLE_HIDE, %OS_SHELL_SYNC)
OutputText(lProgResult + ", ")
Delay(250)
OutputText("power on, ")
I get the following output:
Cycle 1 reprogramming = Success
0, power on,
so there is something special about running within TB as compared to just running in a normal cmd window. I have tried different values for the 'WindowStyle' parameter in OS_Shell() as well, with no change in effect.
This is on a Win 7 Pro machine.
This is *not* a big deal to me at all. It's purely a cosmetic thing in the output of my program. Everything is actually working just fine, it's just not as pretty as it could be.
*Brian
ErosOlmi
07-03-2018, 23:11
Is your thinBasic script a .tBasic or .tbasicc extension?
When using .tbasic extension you are executing thinbasic.exe GUI version (even if you use "Console" module)
When using .tbasicc extension you are executing thinbasicc.exe Console version
ErosOlmi
08-03-2018, 00:12
Let's see if I get it
Attached File and Console module updated. Download and unzip in \thinBasic\Lib\ directory replacing your current one.
I've added a new File function (File_Attr) and fixed an undocumented Console function (Console_SetStdHandle) that I will release in next thinBasic version
Mainly before you want to avoid to get standard output of you shelled application be printed in your console screen, you create a dummy file and redirect standard output into that dummy file
Let me know if it works
Here an example on how to proceed
uses "Console"uses "OS"
uses "file"
long X
long lProgResult
'---Redirect standard output into a dummy file
LOCAL hFile AS LONG
local hSys AS LONG
string sOutFile = "My_stdout.txt"
hFile = FILE_Open(sOutFile, "output")
hSys = FILE_Attr(hFile,2) ' get system handle
Console_SetStdHandle(%CONSOLE_STD_OUTPUT_HANDLE, hSys) ' make this file the STDOUT for this process
for x = 1 to 2
OutputText("Cycle " + Using$("####",x) + " reprogramming = ")
' Send command to Cyclone to reprogram motherboard
lProgResult = OS_Shell("_______HERE YOUR SHELL COMMAND _____________", %OS_WNDSTYLE_HIDE, %OS_SHELL_SYNC)
OutputText(lProgResult + ", ")
Delay(250)
OutputText("power on, ")
Next
'---Close redirection
FILE_Close(hFile)
OutputText("---End---")
WaitKey
function OutputText(byval sText as string)
printl sText
end Function
function delay(byval mSec as long)
sleep mSec
end Function
EmbeddedMan
08-03-2018, 16:30
Is your thinBasic script a .tBasic or .tbasicc extension?
When using .tbasic extension you are executing thinbasic.exe GUI version (even if you use "Console" module)
When using .tbasicc extension you are executing thinbasicc.exe Console version
Ah! Maybe that's the ticket! I was using .tbasic.
However, if I switch to .tbasicc, and run it, I get the following:
D:\Projects\CSIEE\PIGrapher>CycloneCRCCorruptionTest.tbasicc
Programming CRC Corruption Test program v1.0 started on 2018-03-08 08:00:44
Opening relay COM84
...open relay port ok.
Opening command COM4
...open command port ok.
Cycle 1 reprogramming = Success
0, power on, power off, delaying 2s, testing, power off passed.
Cycle 2 reprogramming = Success
0, power on, power off, delaying 2s, testing, power off passed.
Command COM port now closed
Relay COM port now closed
Press any key to exit
Note the "Success", so that change didn't solve the problem.
*Brian
EmbeddedMan
08-03-2018, 16:43
Let's see if I get it
Attached File and Console module updated. Download and unzip in \thinBasic\Lib\ directory replacing your current one.
I've added a new File function (File_Attr) and fixed an undocumented Console function (Console_SetStdHandle) that I will release in next thinBasic version
Mainly before you want to avoid to get standard output of you shelled application be printed in your console screen, you create a dummy file and redirect standard output into that dummy file
Let me know if it works
And you've gone and done it again Eros! This solution totally solves the problem. When I followed your instructions above, I get the following as output:
Programming CRC Corruption Test program v1.0 started on 2018-03-08 08:41:35
Opening relay COM84
...open relay port ok.
Opening command COM4
...open command port ok.
Cycle 1 reprogramming = 0, power on, power off, delaying 2s, testing, power off passed.
Cycle 2 reprogramming = 0, power on, power off, delaying 2s, testing, power off passed.
Command COM port now closed
Relay COM port now closed
Press any key to exit
Which is exactly how it should be. Nice and clean.
Thank you SO MUCH!!
*Brian
ErosOlmi
08-03-2018, 18:53
Thanks for confirming it is working.
I will document and better test new functionalities in next thinBasic.