批处理 - 我怎样才能重定向标准错误和stdout从批处理脚本本身? [英] Batch - How can I redirect stderr and stdout from within the batch script itself?

查看:576
本文介绍了批处理 - 我怎样才能重定向标准错误和stdout从批处理脚本本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Unix的shell脚本就可以重定向从stderr和标准输出脚本本身如下内:

 #!/ bin中/ KSH
#SCRIPT_NAME:test.sh
出口AUTO_LOGFILE =`基名$ 0个.sh`.log
#输出和错误重定向。这将节省FD 3旧标准输出,并在FD 4老标准错误。
EXEC 3 GT;&放大器; 0 4是H.&放大器1为卤素;> $ AUTO_LOGFILE 2 - ;&放大器; 1
回声的Hello World
#以上的回波将被打印到test.log中

在效果,test.sh可以简单地执行为:

  test.sh

而不是:

  test.sh>> test.log中2  - ;&放大器; 1

我试图做批处理脚本类似。我的批处理code是如下:

 关闭@echo&放大器; SETLOCAL ENABLEEXTENSIONS enabledelayedexpansion
REM SCRIPT_NAME = test.bat的
设置AUTO_LOGFILE =%〜n0.log
REM如何做好stdout和stderr重定向从脚本本身在这里?

我怎样才能重定向标准错误和stdout从批处理脚本本身?什么我更感兴趣的是这个转换UNIX shell脚本语句转换成等效的批处理code:

  EXEC 3 GT;&放大器; 0 4 GT;&放1 GT;> $ AUTO_LOGFILE 2  - ;&放大器; 1


解决方案

test.bat的下面批处理文件的功能上等同的到Unix脚本,也就是说,它将所有标准输出到日志文件和错误输出到屏幕上:

 关闭@echo
如果定义再入转到折返
设置折返= TRUE设置AUTO_LOGFILE =%〜N0.log
REM标准输出重定向。离开STDERR原样。%〜F0%*>>%AUTO_LOGFILE%:再入
集再入=
回声的Hello World
雷姆以上的回波将被打印到test.log中
REM和错误信息将显示在屏幕上:
验证badparam

Adenddeum:

我以为OP想要的独立的标准错误,从标准输出看到错误信息在屏幕上,但也许我误会了他。为了标准输出和标准错误发送到文件,请使用下表dbenham在他的评论指出,行:

 %〜F0%*>>%AUTO_LOGFILE%2  - ;&放大器; 1

,或在上面的评论已经建议了更简单的方法:

 关闭@echo
设置AUTO_LOGFILE =%〜N0.log
REM输出和错误重定向。拨打:主要%*>>%AUTO_LOGFILE%2 - ;&放大器; 1
GOTO:EOF:主要
回声的Hello World
雷姆以上的回波将被打印到test.log中

但是,如果目的是要的 distingish 的错误消息,从正常输出,那么它可能做的在同一屏幕的通过这种伎俩:

 %〜F0%* 2  - ;&放大器; 1 1 GT;及3 | FINDSTR / N / A:4E^

这样的错误信息出现一个行号黄色pceded红色背景上$ P $。这种方法可以在一些情况下可直接使用;例如,在任何编程语言源程序的编译:

  anycompiler%1 2  - ;&放大器; 1 1 GT;及3 | FINDSTR / N / A:4E^

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

解决方案

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 "^"

这篇关于批处理 - 我怎样才能重定向标准错误和stdout从批处理脚本本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆