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