为什么set / p从管道仅获得1行?

问题描述 投票:0回答:1

我正在尝试做类似的事情

type test.txt | multiline.bat

with

test.txt

1
2
3

multiline.bat

set /p a=a
set /p "b=b"
set /p c=c

(echo %a% %b% %c%)>result.txt
pause

但是result.txt保留了

1

当我期望的时候

1 2 3

我找到了https://stackoverflow.com/a/6980605,其中说

set /p doesn't work with pipes, it takes one (randomly) line from the input.

但是为什么?

windows batch-file pipe stdout stdin
1个回答
0
投票

set /p在处理换行符时停止读取输入。这可以通过简单的脚本看到]

@echo off
set /p "line=String containing newline:"
echo %line%

然后粘贴字符串

one
two

仅显示one


如果知道需要处理多少行,则可以将set /p命令组合到一个代码块中,然后将文件重定向到它,如下所示:
(
    set /p "a="
    set /p "b="
    set /p "c="
)<test.txt
(echo %a% %b% %c%)>result.txt

您也可以将输入文件的名称作为参数,并在该代码段中将test.txt更改为%1

© www.soinside.com 2019 - 2024. All rights reserved.