如何将一个输出重定向到另一个输入

如何将一个输出重定向到另一个输入

本文介绍了在本机窗口中运行两个命令提示符终端,如何将一个输出重定向到另一个输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在运行的Windows 10命令提示符,正在等待输入,我希望通过第二个命令提示符自动进行连续的实时输入.我已经获得了第二个命令提示符以提取所需的变量,并且希望将其发送到另一个等待输入的命令提示符.

I have one Windows 10 command prompt running and awaiting input, and I wish to automate continuous and live input with a second command prompt. I have gotten the second command prompt to extract the desired variable, and I wish to send it to the other command prompt that is waiting for input.

等待输入"命令提示符必须实时运行,因为它已连接到连接到微控制器的Plink(不是SSH会话,因此此处不使用-m命令).因此,它不能通过函数调用来实现(至少我不认为).

The "awaiting input" command prompt must run in real time because it is connected to Plink (not an SSH session so no use of the -m command here) which is connecting to a microcontroller. So it cannot be accomplished (at least I don't think) with function calls.

我看到它可以在UNIX环境中完成:

I see that it can be done in UNIX environments: https://askubuntu.com/questions/496914/write-command-in-one-terminal-see-result-on-other-one

请先谢谢,

-一个充满希望的初学者

--A hopeful beginner

推荐答案

批处理代码启动2个管道处理,一个用于获取键盘输入并写入文件,另一个用于读取写入的数据.请注意,每个进程都没有一个cmd窗口,但是正在运行两个新进程.如果需要两个控制台,则可以使用 cmd/c start .

Batch code starts 2 piped processes, one for getting keyboard input and writing to a file, and the other reading the data written. Note there isn't a cmd window for each process, but there are two new processes running. You may use cmd /c or start if you need two consoles.

有帮助吗?

@echo off

set "pipefile=pipefile.txt"

if "%~1" neq "" goto %1

copy nul "%pipefile%" >nul
"%~F0" getInput >>"%pipefile%" | "%~F0" readInput <"%pipefile%"
echo(
echo(Batch end...
ping localhost -n 1 >nul
del /F /Q "%pipefile%" 2>nul
exit/B



:getInput
set "input="
set/P "input="<CON
echo(%input%
if /I "%input%" equ "EXIT" exit
goto:getInput


:readInput
setlocal enableDelayedExpansion
set/P="enter some data [EXIT to exit] "
for /l %%. in () do (
  set "input="
  set/P "input="
  if defined input (
    if /I "!input!" equ "EXIT" exit
    set/P="enter some data [EXIT to exit] "
  )
   ping 1.1.1.1 -w 10 -n 1 >nul 2>nul & rem avoid processor load (may be removed)
)

运行两个控制台窗口

@echo off

set "pipefile=pipefile.txt"

if "%~1" neq "" goto %1

del /F /Q "%pipefile%" 2>nul
copy nul "%pipefile%" >nul

start "writer" cmd /c ^""%~f0" readInput ^<"%pipefile%" ^"
start "reader" cmd /c ^""%~f0" getInput ^"

echo(
Echo batch end...
ping localhost -n 1 >nul
goto :EOF


:getInput
set "input="
set/P "input=enter some data [EXIT to exit] "
echo(%input%>>"%pipefile%"
if /I "%input%" equ "EXIT" exit
goto:getInput

:readInput
setlocal enableDelayedExpansion
for /l %%. in () do (
  set "input="
  set /p "input="
  if defined input (
    if /I "!input!" equ "EXIT" exit
    echo(!input!
  )
   ping 1.1.1.1 -w 10 -n 1 >nul 2>nul & rem avoid processor load (may be removed)
)

这篇关于在本机窗口中运行两个命令提示符终端,如何将一个输出重定向到另一个输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:57