3

In Unix shell scripting it is possible to redirect the stderr and stdout from the within script itself as below:

#!/bin/ksh
# script_name: test.sh
export AUTO_LOGFILE=`basename $0 .sh`.log
# stdout and stderr Redirection. This will save the old stdout on FD 3, and the old stderr on FD 4.
exec 3>&0 4>&1 >>$AUTO_LOGFILE 2>&1
echo "Hello World"
# The above echo will be printed to test.log

In effect, test.sh can be executed simply as:

test.sh

Instead of:

test.sh >> test.log 2>&1    

I am trying to do the similar in batch script. My batch code is as below:

@echo off & setlocal enableextensions enabledelayedexpansion
REM script_name=test.bat
set AUTO_LOGFILE=%~n0.log
REM How to do the stdout and stderr redirection from within the script itself here?

How can I redirect stderr and stdout from within the batch script itself? What I'm more interested in is converting this unix shell script statement into equivalent batch code:

exec 3>&0 4>&1 >>$AUTO_LOGFILE 2>&1
Sachin S
  • 396
  • 8
  • 17
  • Your question is a duplicate from this: [Is it possible to redirect the output of a batch file inside the script?][1] [1]: http://stackoverflow.com/questions/4607390/is-it-possible-to-redirect-the-output-of-a-batch-file-inside-the-script – Mac Apr 20 '12 at 03:03
  • @Mac I need to execute many commands and call many subroutines in the latter part of the script. I can not simply do that in the function testitout as mentioned in the answer in the link which you provided. – Sachin S Apr 20 '12 at 03:09
  • Not sure about it, but if you had a subroutine, just like your main that calls the other subroutines, would not that work? – Mac Apr 20 '12 at 03:35
  • @MAC Hmm... will look into that. But what I'm more interested in is conversion of this unix shell script statement into equivalent batch code: exec 3>&0 4>&1 >>$AUTO_LOGFILE 2>&1 – Sachin S Apr 20 '12 at 04:05
  • I thought 0 was stdin, 1 stdout, and 2 stderr. So shouldn't your exec line read `exec 3>&1 4>&2 >>$AUTO_LOGFILE 2>&1`? – dbenham Apr 24 '12 at 02:54
  • @SachinS: Just answer if you want to send both stderr and stdout to the file, or just one of them and the other to the screen (which ones?)... – Aacini Apr 24 '12 at 05:15
  • @Aacini Both should be sent to the same file. – Sachin S Apr 26 '12 at 13:30

2 Answers2

5

The test.bat Batch file below is functionally equivalent to your Unix script, that is, it send all standard output to the log file and error output to the screen:

@echo off
if defined reEntry goto reEntry
set reEntry=TRUE

set AUTO_LOGFILE=%~N0.log
rem stdout Redirection. Leave stderr as is.

"%~F0" %* >>%AUTO_LOGFILE%

:reEntry
set reEntry=
echo "Hello World"
rem The above echo will be printed to test.log
rem and error messages will be printed to the screen:
verify badparam

Adenddeum:

I thought the OP wants to separate stdout from stderr to see error messages in the screen, but perhaps I misunderstood him. To send both stdout and stderr to the file, use the line below as dbenham noted in his comment:

"%~F0" %* >>%AUTO_LOGFILE% 2>&1

Or in the even easier way already suggested in a comment above:

@echo off
set AUTO_LOGFILE=%~N0.log
rem stdout and stderr Redirection.

call :Main %* >>%AUTO_LOGFILE% 2>&1
goto :EOF

:Main
echo "Hello World"
rem The above echo will be printed to test.log

However, if the purpose is to distingish error messages from normal output, then it may be done in the same screen via this trick:

"%~F0" %* 2>&1 1>&3 | findstr /N /A:4E "^"

This way the error messages appears preceded by a line number in yellow color on red background. This method may be directly used in several cases; for example, in the compilation of any programming language source program:

anycompiler %1 2>&1 1>&3 | findstr /N /A:4E "^"
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • 1
    +1 But, I think the OP wants to redirect stderr to the same file, so `"%~F0" %* >>%AUTO_LOGFILE% 2>&1`. Also, Windows automatically saves the redirected handles to the first available unassigned handles. So unless prior redirections have occurred, the original stdout is saved in handle 3 and stderr in handle 4. So `echo This will go to the console (stdout) >&3` will do just that. – dbenham Apr 24 '12 at 02:58
1

Enclose the lines in parentheses

(
  REM How to do the stdout redirection from within the script itself here?
  ECHO is this redirected?
  ECHO yes it is
) >out.txt
PA.
  • 28,486
  • 9
  • 71
  • 95