看来都不会搜索啊
http://www.cn-dos.net/forum/viewthread.php?tid=22281&fpage=1&highlight=tee.&page=2
willsort
金牌会员
Batchinger
积分 4224
发帖 1512
注册 2002-10-18
状态 离线 『第 19 楼』:
Quote:
Originally posted by 3742668 at 2006-8-5 18:45:
知识面有多大,脚本的功能就有多大。
Re acoreq:
诚然!我们选择解决问题的方法,取决于我们如何理解问题本身。
在你的应用中,使用&或者不使用&分别执行两次命令将输出分别重定向,可能是最简单的方案,不管它有多么繁琐,它至少简单,而简单与简洁并不等同。
========================================
但可能你会遇到一些预期的问题,比如某些命令第一次执行与第二次执行的输出往往并不相同,比如 echo.%time% ,此时分两次执行将得到不同的结果,他们被分别重定向了到文件和控制台上。
这并不是我们想要的结果,所以我们需要改进。最先能想到的,我们可以将所有的命令输出首先重定向到文件,然后在最护 type 这个文件到控制台上。这似乎是个不错的方案,至少它解决了第一个问题,而且也足够简单。
dir > cmd.log
cd >> cmd.log
...
type cmd.log
========================================
但仍然有新的问题,比如在很多时候,我们希望在执行了某些命令后,能够立即查看它的输出,我们可以此时就 type 这个文件,但如果以后有新的命令输出需要重定向,我们就面临两难:如果在查看完旧的命令日志后删除它,则日志文件将缺少前面的命令输出;而如果不删除,继续用>>添加新的内容,则在随后的 type 时,前面的命令输出将再次输出到屏幕上。
因此改进需要继续,或许将命令日志由一个拆成多个是个办法——每个命令都有单独的命令日志,它们可以独立的重定向并且输出,而不会相互掣肘。最后,如果我们想得到一个完整的日志文件,只需要用 copy 简单的合并一下即可。这个方案仍然足够简单,而且可以解决目前面临的问题, 除非有新的问题。
dir > _dir.log
type _dir.log
cd > _cd.log
type _cd.log
...
copy _*.log cmd.log >nul
del _*.log
========================================
不过,如果你自命是一个很爱钻研(较真的同义词)的批处理设计者,因此对以上的方案都抱有某种不满(比如第三方案中的临时日志文件),我们就需要寻找更合适的方案。
首先,我会想到一个名为 tee (很古怪的名字)的 Unix 工具,它是将标准输出的文本输出到控制台的同时输出到文件中(其中未涉及重定向的概念,但本质上是类似的机制)的标准工具,我们可以很轻易的获得这个工具的DOS或者Windows移植版。
它的用法很简单,在中曾有提及
dir | tee cmd.log
cd | tee --append cmd.log
========================================
但如果“较真的你”对第三方工具比较抵触,或者有无法使用的苦衷(比如如如何保持跨平台的兼容性),那么继续寻找新的方案。
很快你就会发现William Allen的vbs脚本,这一个tee.exe的第一方实现,也就是说我们用vbs脚本完成了类似tee的功能。
Set StdIn = WScript.StdIn
Set StdOut = WScript.StdOut
Set Args=WScript.Arguments
LogFile=Args(0)
Set fso = CreateObject("Scripting.FileSystemObject")
Set LogFile= fso.CreateTextFile(Args(0))
Do While Not StdIn.AtEndOfStream
str = StdIn.ReadLine
StdOut.WriteLine str
LogFile.WriteLine str
Loop
LogFile.Close
将这个脚本保存为 tee.vbs ,然后我们就可以使用下面的方法保存命令的输出了。当然我们可以简化或增强这个脚本,比如让它可以将命令输出添加到日志文件中,而不是覆盖。
dir | cscript//nologo tee.vbs cmd.log
========================================
最后,如果你是纯DOS的拥护者,对vbs脚本并不感冒。那么还会有一个ASCode等待你去发现。这是ASCII Assembler技术专家 Herbert Kleebauer 的又一款ASCode力作。
echo Bj@jzh`0X-`/PPPPPPa(DE(DM(DO(Dh(Ls(Lu(LX(LeZRR]EEEUYRX2Dx=>tee.com
echo 0DxFP,0Xx.t0P,=XtGsB4o@$?PIyU WwX0GwUY Wv;ovBX2Gv0ExGIuht6>>tee.com
echo @VyI?@xAp~sA`LZNxOq@Kt@FB?sUs`LbLB?tgj`{gjB~0x>>tee.com
这个tee.com的用法也并不会比tee.exe和tee.vbs复杂多少,而且在 DOS/9X/NT 下有一致的表现,除了这个16位的.com程序会影响CMD@NT的代码页之外,而且有些“拒人千里”的晦涩难懂之外,它应该还算是一个Pefect的方案。
dir | tee > cmd.log
cd | tee >>cmd.log
========================================
TEE.EXE
http://www.cn-dos.net/forum/atta ... 62e9&download=1
如何实现既在屏幕上显示执行结果又可以把结果保存到
http://www.cn-dos.net/forum/viewthread.php?tid=21531
alt.msdos.batch.nt > Can you use redirection to two outputs?
http://groups.google.com/group/a ... sg/514d1343700b423e
alt.msdos.batch.nt > Tee
http://groups.google.com/group/a ... sg/b157065b8b12ebb2
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
It seems no one knows how to search.
http://www.cn-dos.net/forum/viewthread.php?tid=22281&fpage=1&highlight=tee.&page=2
willsort
Gold Member
Batchinger
Points 4224
Posts 1512
Registered 2002-10-18
Status Offline 『Post 19』:
Quote:
Originally posted by 3742668 at 2006-8-5 18:45:
The breadth of your knowledge determines the functionality of your scripts.
Re acoreq:
Indeed! The method we choose to solve a problem depends on how we understand the problem itself.
In your application, executing the command twice with or without & to redirect the output respectively may be the simplest solution. No matter how tedious it is, it is at least simple, and simplicity is not the same as conciseness.
========================================
But you may encounter some expected problems. For example, the output of some commands executed twice at different times is often different. For example, echo.%time%, executing it twice will get different results, and they are redirected to the file and the console respectively.
This is not the result we want, so we need to improve. The first thing that comes to mind is that we can first redirect all command output to a file, and then finally type this file to the console. This seems like a good solution. At least it solves the first problem, and it is also simple enough.
dir > cmd.log
cd >> cmd.log
...
type cmd.log
========================================
But there are still new problems. For example, in many cases, we want to be able to immediately view the output of some commands. We can type this file at this time, but if there are new command outputs to be redirected later, we face a dilemma: if we delete the command log after viewing the old command log, the log file will lack the previous command output; and if we don't delete it and continue to add new content with >>, then when we type later, the previous command output will be output to the screen again.
Therefore, the improvement needs to continue. Perhaps splitting the command log into multiple ones is a way - each command has a separate command log, which can be redirected and output independently without interfering with each other. Finally, if we want to get a complete log file, we just need to simply merge it with copy. This solution is still simple enough and can solve the current problems, unless there are new problems.
dir > _dir.log
type _dir.log
cd > _cd.log
type _cd.log
...
copy _*.log cmd.log >nul
del _*.log
========================================
However, if you consider yourself a very inquisitive (a synonym for being picky) batch processing designer, and therefore have some dissatisfaction with the above solutions (such as the temporary log files in the third solution), we need to find a more suitable solution.
First, I will think of a Unix tool called tee (a very strange name). It is a standard tool that outputs the text of the standard output to the console while outputting it to a file (it does not involve the concept of redirection, but essentially it is a similar mechanism). We can easily obtain the DOS or Windows port version of this tool .
Its usage is very simple, which was mentioned in
dir | tee cmd.log
cd | tee --append cmd.log
========================================
But if the "picky you" is resistant to third - party tools or has unavoidable difficulties (such as how to maintain cross - platform compatibility), then continue to find a new solution.
Soon you will find William Allen's vbs script , which is a first - party implementation of tee.exe, that is, we use the vbs script to complete the function similar to tee.
Set StdIn = WScript.StdIn
Set StdOut = WScript.StdOut
Set Args=WScript.Arguments
LogFile=Args(0)
Set fso = CreateObject("Scripting.FileSystemObject")
Set LogFile= fso.CreateTextFile(Args(0))
Do While Not StdIn.AtEndOfStream
str = StdIn.ReadLine
StdOut.WriteLine str
LogFile.WriteLine str
Loop
LogFile.Close
Save this script as tee.vbs, and then we can use the following method to save the output of the command. Of course, we can simplify or enhance this script. For example, let it be able to add the command output to the log file instead of overwriting it.
dir | cscript//nologo tee.vbs cmd.log
========================================
Finally, if you are a pure DOS enthusiast and are not keen on vbs scripts. Then there will be an ASCode waiting for you to discover . This is another ASCode masterpiece by ASCII Assembler technology expert Herbert Kleebauer.
echo Bj@jzh`0X-`/PPPPPPa(DE(DM(DO(Dh(Ls(Lu(LX(LeZRR]EEEUYRX2Dx=>tee.com
echo 0DxFP,0Xx.t0P,=XtGsB4o@$?PIyU WwX0GwUY Wv;ovBX2Gv0ExGIuht6>>tee.com
echo @VyI?@xAp~sA`LZNxOq@Kt@FB?sUs`LbLB?tgj`{gjB~0x>>tee.com
The usage of this tee.com is not much more complicated than tee.exe and tee.vbs. And it has consistent performance under DOS/9X/NT. Except that this 16 - bit.com program will affect the code page of CMD@NT, and it is a bit difficult to understand, it should still be a perfect solution.
dir | tee > cmd.log
cd | tee >>cmd.log
========================================
TEE.EXE
http://www.cn-dos.net/forum/atta ... 62e9&download=1
How to display the execution result on the screen and save the result to
http://www.cn-dos.net/forum/viewthread.php?tid=21531
alt.msdos.batch.nt > Can you use redirection to two outputs?
http://groups.google.com/group/a ... sg/514d1343700b423e
alt.msdos.batch.nt > Tee
http://groups.google.com/group/a ... sg/b157065b8b12ebb2
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!