0

为什么“ECHO is off”出现在这里并破坏了一切?

源代码:

set authfile=\\COMPUTER\Users\username\auth.txt
echo Authentication file not detected.
echo.
echo Press enter to generate a new one...
pause > nul
echo 4.7>"%authfile%"
echo 0>>"%authfile%"
echo sigh>>"%authfile%"
echo 0>>"%authfile%"
echo 0>>"%authfile%"
echo 0>>"%authfile%"
echo 0>>"%authfile%"
echo 0>>"%authfile%"
if exist "%authfile%" goto success
goto failure

输出:

Authentication file not detected.

Press enter to generate a new one...
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.

授权文件:

4.7
sigh

是的,所有的 0 必须以正确的顺序和位置输出。

4

3 回答 3

4

0>>表示您正在尝试将文件描述符 0(即 STDIN)重定向到输出文件。正如Endoro建议的那样,要么将重定向放在行首:

>>"%authfile%" echo 0

或转义数字:

echo ^0>>"%authfile%"
于 2013-08-11T11:53:43.380 回答
3

put the echo command behind the redirection:

>"%authfile%" echo 4.7
>>"%authfile%" echo 0
...
于 2013-08-11T11:45:45.480 回答
3

您的六个“ECHO 已关闭”消息的原因是由于您编写了以下行:

echo 0>>"%authfile%"

该语句的正确形式是(注意“0”和“>>”之间的空格):

echo 0 >>"%authfile%"

技术说明

错误在于 DOS 批处理语言重定向语法。您可以分别使用数字 1 或 2将输出重定向到stdoutstderr :

echo Hi! 1>"%authfile%"
echo Hi! 2>"%authfile%"

因为您在案例中省略了空格,所以 DOS 将“0”解析为重定向号,其中 0 对应于stdin。由于到标准输入的管道没有意义,DOS 批处理解析器忽略了管道,给您留下:

echo

没有“。” 回声后,您将收到消息“ECHO 已关闭。”。

于 2013-08-11T11:48:42.963 回答