Running two command prompt terminals in native windows, how can I redirect the output of one to the input of the other?

ぃ、小莉子 提交于 2021-01-29 08:04:04

问题


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.

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.

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

Thanks in advance and please advise,

--A hopeful beginner


回答1:


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.

Does it help?

@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) 
)

Running two console windows

@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) 
)


来源:https://stackoverflow.com/questions/39419420/running-two-command-prompt-terminals-in-native-windows-how-can-i-redirect-the-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!