Unixシェルスクリプトでは、次のように、スクリプト自体からstderrとstdoutをリダイレクトできます。
#!/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
実際、test.shは次のように簡単に実行できます。
test.sh
それ以外の:
test.sh >> test.log 2>&1
私はバッチスクリプトで同様のことをしようとしています。私のバッチコードは次のとおりです。
@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?
バッチスクリプト自体からstderrとstdoutをリダイレクトするにはどうすればよいですか?私がもっと興味を持っているのは、このUNIXシェルスクリプトステートメントを同等のバッチコードに変換することです。
exec 3>&0 4>&1 >>$AUTO_LOGFILE 2>&1