从使用“start”的 cmd.exe 读取 Pipe内置
我正在尝试使用 WScript.exe 为当前用户(在 Windows 上)“留言”,而不等待用户确认该消息。
我的想法是创建一个新进程,使用“start”内置函数(它通常会打开一个新窗口,执行控制权返回给调用者),并使用一个简短的脚本调用 WScript 以在屏幕上打开一个消息框,然后退出。
除非我尝试从该脚本打开管道(并读取它),否则这工作正常。如果我尝试从管道读取,则读取 I/O 永远不会获得 EOF,直到 wscript.exe 退出,即使该程序在“start”命令之后继续执行也是如此。
下面是一个示例:
test.vbs
WScript.Echo "Hello World!"
invoke.cmd
start wscript.exe //nologo test.vbs
@echo Exiting...
exit /b
如果我在命令提示符下运行 invoke.cmd,WScript 窗口将打开并且控制权将返回到提示符。
但是,如果我尝试将输出通过管道传输到命令(尝试读取管道),则子命令似乎永远不会看到管道已关闭,直到 wscript.exe 退出。
但是,此行为不需要 WScript.exe。只要“start”创建的进程仍在运行,它似乎就会以这种方式运行。我可以使用“cmd.exe /k”获得相同的行为。
invoke2.cmd
start cmd /k date /t
@echo Exiting...
exit /b
尝试从 invoke2.cmd 的管道中读取数据会阻塞,直到“start”进程结束。
我没有看到任何“启动”命令行参数声称可以管理这样的行为,而且我真的不明白为什么使用“启动”创建新进程/窗口的 cmd.exe 不会退出,直到“开始”窗口退出。
奇怪的是,如果我将输出重定向到文件,事情就会起作用。它只是不适用于管道。
有什么想法吗?
I'm attempting to "leave a message" for the current user (on Windows) using WScript.exe, without waiting for the user to acknowledge the message.
My thought was to create a new process, using the "start" builtin (it usually opens a new window and execution control returns to the caller) and call WScript with a short script to open a message box on the screen and then exit.
This works OK unless I attempt to open a pipe from that script (and read it). If I attempt to read from the pipe, the read I/O never gets EOF until wscript.exe exits, even though that program continues execution after the "start" command.
Here's an example:
test.vbs
WScript.Echo "Hello World!"
invoke.cmd
start wscript.exe //nologo test.vbs
@echo Exiting...
exit /b
If I run invoke.cmd in a command prompt, the WScript window opens and control returns to the prompt.
However, if I attempt to pipe the output to a command (that attempts to read the pipe), the subcommand never seems to see that the pipe has closed until wscript.exe exits.
This behavior doesn't require WScript.exe, however. It seems to behave this way as long as the process created by "start" is still running. I can get the same behavior with "cmd.exe /k".
invoke2.cmd
start cmd /k date /t
@echo Exiting...
exit /b
Attempting to read from a pipe from invoke2.cmd blocks until the "start"-ed process ends.
I don't see any command-line args to "start" that claim to govern behavior like this, and I don't really understand why the cmd.exe that's using "start" to create new processes/windows won't exit until the "start"-ed window exits.
Curiously enough, things work if I redirect the output to a file. It just doesn't work with a pipe.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您使用的是 wscript.exe,因此它会在您的 Echo 命令上弹出一个消息框。尝试运行
start cscript.exe //nologo test.vbs
cscript.exe 是命令行版本,因此它会输出到命令行。
Since you are using wscript.exe, it is popping up a message box on your Echo command. Try running
start cscript.exe //nologo test.vbs
cscript.exe is the command line version, so it will output to the command line.