I'm currently experimenting with pipes. So I created a simple batch file (dos/windows) as follows:
@echo off
echo [+] starting batch file
:start
set /p msg="[+] enter msg: "
echo [+] Your message: %msg%
IF "%msg%"=="x" (
echo [x] end loop
goto exit
) ELSE (
goto start
)
:exit
echo [+] bye
This works fine as long as I do call it from the commandline:
> showAll.bat
S:\80_personalFolder\81_lab\python\ghoul>showAll.bat
[+] starting batch file
[+] enter msg: hello
[+] Your message: hello
[+] enter msg: x
[+] Your message: x
[x] end loop
[+] bye
S:\80_personalFolder\81_lab\python\ghoul>
But as soon as I try to pipe input to it it will run in an indefinite loop:
> echo hello | showAll.bat
will result in:
[...]
[+] enter msg: [+] Your message: hello
[+] enter msg: [+] Your message: hello
[+] enter msg: [+] Your message: hello
[+] enter msg: [+] Your message: hello
[+] enter msg: [+] Your message: hello
[...]
I dont understand that behaviour. Could someone explain what I oversee or what I'm missing! How would I fix that, so it wont run indefinitely but will stop with the next prompt within the loop, so I can exit manually?
UPDATE! First of all thanks a lot for the really good answers I received. It helped me a lot to better understand what happened. So I modified the code to reset the input var before read is called again. What I would like to achieve would be the following: 1) piping 'echo hello' to the batchscript 2) once the script ran hello from the pipe input and reenters the loop... 3) ... I would like to be able to manually input something else . instead it will still loop indefinetely with whatever default value the script will set the variable to. how can I stop the piping and switch back to the default stdin ? I guess thats the real issue I'm struggeling with. I'm not even sure this can be done.
Thanks and Best