Capture and display STDOUT at the same time

拥有回忆 提交于 2019-11-28 10:18:41
ConsoleWrite(_getDOSOutput('ipconfig /all') & @CRLF)

Func _getDOSOutput($command)
    Local $text = '', $Pid = Run('"' & @ComSpec & '" /c ' & $command, '', @SW_HIDE, 2 + 4)
    While 1
            $text &= StdoutRead($Pid, False, False)
            If @error Then ExitLoop
            Sleep(10)
    WEnd
    Return StringStripWS($text, 7)
EndFunc   ;==>_getDOSOutput

Maybe this helps you out.

Something similar to the linux tee command which logs to file while it prints STDOUT.

Redirecting STDOUT makes receiving party (the script) responsible for display and logging. As per documentation:

StdoutRead() does not block, it will return immediately. In order to get all data, it must be called in a loop.

Example:

#AutoIt3Wrapper_Change2CUI=Y

#include <Constants.au3>
#include <MsgBoxConstants.au3>

Global Enum  $EXIT_OK, _
             $EXIT_NOCOMPILE, _
             $EXIT_ABORT

Global Const $g_sPromptError  = 'Compile this script and run resulting executable instead.', _
             $g_sPromptInput  = 'Enter a command:', _
             $g_sInputDefault = 'ping localhost -n 10'

Global       $g_sCMD = '', _
             $g_sSTD = ''

Main()

Func Main()

    If Not @Compiled Then

        MsgBox($MB_OK + $MB_ICONERROR, @ScriptName, $g_sPromptError)
        Exit $EXIT_NOCOMPILE

    EndIf

    $g_sCMD = InputBox(@ScriptName, $g_sPromptInput, $g_sInputDefault)
    If @error Then Exit $EXIT_ABORT

    $g_sSTD = _getCmdStd($g_sCMD)
    MsgBox($MB_OK + $MB_ICONINFORMATION, $g_sCMD, $g_sSTD)

    Exit $EXIT_OK
EndFunc

Func _getCmdStd(Const $sCMD, Const $sDir = '', Const $iType = $STDERR_MERGED, Const $bShow = False, Const $iDelay = 100)
    Local       $sTMP = ''
    Local       $sSTD = ''
    Local       $sCOM = @ComSpec & ' /c ' & $sCMD
    Local Const $iWin = $bShow ? @SW_SHOW : @SW_HIDE
    Local Const $iPID = Run($sCOM, $sDir, $iWin, $iType)

    While True

        $sTMP = StdoutRead($iPID, False, False)

        If @error Then

            ExitLoop 1

        ElseIf $sTMP Then

            $sTMP  = StringReplace($sTMP, @CR & @CR, '')
            $sSTD &= $sTMP

            ConsoleWrite($sTMP)

        EndIf

        Sleep($iDelay)

    WEnd

    Return SetError(@error, @extended, $sSTD)
EndFunc

Returns STDOUT (and STDERR) after execution completes, while writing to console during execution. Replace MsgBox() as required (logging function).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!