为什么在重定向 &0 流时无法获取用户输入

Why I can't get the user input when &0 stream is redirected

示例:

set /p= 0>nul
runas /user:"" "" >nul 2>&1 0>con
comp nul nul 0>nul

命令的执行就像输入一样pressed.I当使用输入重定向而不是输出时,更愿意期待这样的事情。

让我们跟随 MS documentacion.

我们有句柄重复

2>&1   : Take the handle in the stream 1, 
         duplicate it 
         and place the copy as the handle in stream 2.

和重定向运算符

2>file : Get a handle to the file and 
         place the handle to be used as stream 2

<file  : Get a handle to the file and
         place the handle to be used as stream 1 (implicit)

在所有情况下,都会获得一个句柄(复制或不复制)并将其作为指定流的句柄。

现在你的代码

set /p= 0>nul
        ^^^^^ .... What does it mean? 

这意味着:打开一个流到nul"device",并设置这个流的句柄(copy/duplicate它)作为流0

的句柄

所以,这两行是等价的(或多或少,见编辑)

set /p= <nul
set /p= 0>nul

在这两种情况下,存储在 &0 中的句柄指向 nul 设备。

编辑 以适应评论并完成答案。

Not then why set /p= 0<con and set /p= 0>con give different results?

也就是说,第一个从控制台读取,第二个没有。如前所述,如果在这两种情况下,流 &0 都包含同一设备的句柄,那么这种行为的原因是什么?

处理打开的选项。 &0关联的设备是一样的,但是我们得到的句柄不是这样。至少文档是这样说的:

  • < 生成只读句柄
  • > 生成一个只写句柄

所以,

  • 在第一种情况下,可以从句柄读取,因为可以读取控制台并且句柄允许读取
  • 在第二种情况下,可以读取控制台,但无法从句柄中读取,因为它是写句柄