Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: How to redirect output of command called from OS_Shell()?

  1. #1

    How to redirect output of command called from OS_Shell()?

    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

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Try redirect your shell command to NUL

    uses "OS"
    uses "console"
    
    
    OS_Shell("CMD.EXE /C DIR > NUL")
    WaitKey
    
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    Unfortunately, that appeared to have no effect. :-(

    Still get the extra output in my TB console.

    *Brian

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Redirecting into a file?

    If possible send me e little example showing what you are doing to better understand.

    support@thinbasic.com
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6
    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

  7. #7
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    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
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    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
    
    Attached Files Attached Files
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  9. #9
    Quote Originally Posted by ErosOlmi View Post
    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

  10. #10
    Quote Originally Posted by ErosOlmi View Post
    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

Page 1 of 2 12 LastLast

Similar Threads

  1. Console - function to be called at shutdown
    By Petr Schreiber in forum Suggestions/Ideas discussions
    Replies: 2
    Last Post: 12-03-2016, 13:31
  2. OS_Shell - question/issue
    By ReneMiner in forum OS
    Replies: 12
    Last Post: 16-09-2015, 22:44
  3. Replies: 2
    Last Post: 20-11-2014, 15:40
  4. Quote Of the Day Different output?
    By ErosOlmi in forum QOD: Question Of the Day
    Replies: 2
    Last Post: 08-02-2013, 09:44

Members who have read this thread: 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •