Batch:为什么附加文本文件会写"ECHO is off"?反而? [英] Batch: Why does appending a textfile write "ECHO is off" instead?

查看:264
本文介绍了Batch:为什么附加文本文件会写"ECHO is off"?反而?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我编写了一个包含一些代码的批处理,以通过读取文本文件然后将增加的新数字写回到该文本文件中来检查运行了多少次.

So I wrote a batch that has some code to check how many times it has been run by reading a textfile and then writing back into that textfile the new, increased number.

@ECHO OFF
for /f "delims=" %%x in (TimesRun.txt) do set Build=%%x
set Build=%Build%+1
@echo Build value : %Build%
echo %Build%>>TimesRun.txt
Pause

那确实附加了文本文件,但是它添加了"1 + 1".愚蠢的我!我忘了使用/a开关启用算术运算!但是当我相应地更改代码时...

That does append the textfile allright, but it adds "1+1" to it. Silly me! I forgot to use the /a switch to enable arithmetic operations! But when I change the code accordingly...

@ECHO OFF
for /f "delims=" %%x in (TimesRun.txt) do set Build=%%x
set /a Build=%Build%+1
@echo Build value : %Build%
echo %Build%>>TimesRun.txt
Pause

...发生了一些有趣的事情: ECHO已关闭,而不是附加我的文件.被写入控制台.现在,我知道这通常发生在使用ECHO而不带文本或带空变量的情况下.我专门添加了第一个@echo Build value : %Build%来查看变量 Build 是否为空;并非如此,并且计算已正确执行. 我已经知道

... something funny happens: Instead of appending my file, ECHO is off. gets written on the console. Now, I know that this usually happens when ECHO is used without text or with an empty variable. I have added the first @echo Build value : %Build% specifically to see whether the variable Build is empty or not; it is not, and the calculation was carried out correctly. I already figured out that

>>TimesRun.txt (echo %Build%)

确实带来了预期的结果.我还是不明白为什么

does bring the desired result. I still do not understand why

echo %Build%>>TimesRun.txt

但是,

没有.我想念什么?

does not, however. What am I missing?

推荐答案

您无意中指定了重定向句柄.

You are unintentionally specifying a redirection handle.

重定向允许您指定某个句柄,该句柄定义了要重定向的内容:

Redirection allows you to specify a certain handle that defines what is to be redirected:

0     = STDIN  (keyboard input)
1     = STDOUT (text output)
2     = STDERR (error text output)
3 ~ 9 = undefined

对于输入重定向运算符<,默认情况下使用句柄0;默认情况下使用句柄.对于输出重定向运算符>>>,默认句柄为1.

For the input redirection operator <, handle 0 is used by default; for the output redirection operators > and >>, the default handle is 1.

您可以通过在重定向操作符前面放置一个数字来显式指定一个句柄.例如2>定义重定向错误文本输出.

You can explicitly specify a handle by putting a single numeric figure in front of the redirection operator; for instance 2> defines to redirect the error text output.

echo命令行中,当%Build%是单个数字时(例如,例如1),您将无意识地执行此操作:

In your echo command line you are doing exactly this unintentionally, when %Build% is a single numberic digit, like 1 for example:

echo 1>>TimesRun.txt

为避免这种情况,您可以使用以下选项:

To avoid that, you have the following options:

  1. 要反转该语句,使重定向定义首先出现:

  1. To reverse the statement so that the redirection definition comes first:

>>TimesRun.txt echo %Build%

这是进行重定向的最通用,最安全的方法.

This is the most general and secure way of doing redirections.

要将重定向的命令括在括号中:

To enclose the redirected command in parentheses:

(echo %Build%)>>TimesRun.txt

这也可以安全地工作.

SPACE 放在重定向运算符的前面:

To put a SPACE in front of the redirection operator:

echo %Build% >>TimesRun.txt

这也可以,但是echo的输出中包含了附加的 SPACE .

This works too, but the additional SPACE is included in the output of echo.

另请参见以下精彩文章: cmd.exe重定向操作员的顺序和位置.

See also this great post: cmd.exe redirection operators order and position.

这篇关于Batch:为什么附加文本文件会写"ECHO is off"?反而?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆