#1 04 Feb 2020 01:11

kingtermite
Member
Registered: 28 Oct 2016
Posts: 18

Pipe output to file without removing from screen

I am writing  a script that I want to capture all STDOUT and STERR both. I know i can route both to screen with 2>&1, but....

I'd like both streams to go to the screen for viewing by person running the script, but I'd like to capture that output to a file at the same time as well, so I can send it to a logger tool.

Is this possible with piping magic?

Offline

#2 05 Feb 2020 12:56

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,114

Re: Pipe output to file without removing from screen

Try this (untested).

for /f "usebackq tokens=1* delims=:" %%a in (`yourcommand 2^>^&1 ^| findstr /n ".*"`) do (

  (<0 set /p "=%%b" & echo:) >&1
  (<0 set /p "=%%b" & echo:) >&2

) 2>> yourlogfile.txt

Not sure what behaviour you'll see if your command is expecting user input.

Last edited by bluesxman (05 Feb 2020 15:45)


cmd | *sh | ruby | chef

Offline

#3 12 Aug 2020 08:12

Minecraft49
Member
From: Poland
Registered: 08 Aug 2020
Posts: 9
Website

Re: Pipe output to file without removing from screen

Try this (tested smile ):

dir >myfile.txt
type myfile.txt

but if piped command requires user input, it will be taken first, before displaying output. It is the best solution for commands that does not require any input. Code above can work even on Windows and MS-DOS!

Last edited by Minecraft49 (12 Aug 2020 08:15)

Offline

#4 12 Aug 2020 17:51

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,114

Re: Pipe output to file without removing from screen

It's fair comment that that could function as a minimum viable product, but not if you want real-time output from the command.


cmd | *sh | ruby | chef

Offline

Board footer

Powered by FluxBB