Why can't I redirect to two input streams `0<` and `3<` in that order?

前端 未结 2 1546
一生所求
一生所求 2021-01-02 10:35

When I try to redirect multiple files into multiple streams, like in the following example, everything works as expected:

3< stream3.txt 4< stream4.txt         


        
2条回答
  •  甜味超标
    2021-01-02 11:10

    Looks like a bug with the way the streams are being restored after the command has finished. Consider the following batch file:

    0< stream0.txt 3< stream3.txt findstr .
    
    findstr .
    
    

    The first findstr will output the content of stream0.txt as expected.

    The second findstr will unexpectedly output the content of stream3.txt, indicating that stream 0 has unexpectedly become redirected.

    When the batch file finishes, the command shell running it will exit too, because it sees an end of file on what is now the standard input stream.


    Retrying one of my experiments in the light of MC ND's answer, I can now reproduce the same issue with a stream other than stream 3. Consider test7.cmd:

    0< stream0.txt 4< stream3.txt findstr .
    
    findstr .
    
    :done
    

    Run as cmd /c test7 or as cmd /c test7 3< other.txt it does not exhibit the unexpected behaviour, but run as cmd /c "test7 3< other.txt" it does. (That was a facepalm moment; I could have discovered this yesterday if I'd thought about it more carefully. Obviously the initial redirection has to be in the context of same command shell that is running the batch script.)

    So the cause isn't "redirecting stream 3 as well as stream 0" but "redirecting the first free stream as well as stream 0". :-)

提交回复
热议问题