为什么用echo命令给我的脚本另一个输出? [英] Why gives my script an other output with the echo command?
问题描述
输出不是我期望得到的...尤其是输出的第一部分.我
The output is not what I expected to get... especially the first piece of the output. I
认为@令牌出了问题.但我找不到任何东西,也找不到
think it goes wrong with the @ token. But I couldnt find anything about it and cant
把它弄清楚.
有人知道我在做什么错吗?
Does anyone know what I am doing wrong??
THNX
这是我的脚本:
if not exist input.bat (
echo @echo off > input.bat
echo title Input >> input.bat
echo :set >> input.bat
echo MODE CON: COLS=29 LINES=5 >> input.bat
echo :loop >> input.bat
echo cls >> input.bat
echo echo Gebruik de wasd toetsen >> input.bat
echo echo om te bewegen >> input.bat
echo echo a/Left d/Right >> input.bat
echo choice /c:wscradp /n >> input.bat
echo if ERRORLEVEL 6 ( >> input.bat
echo echo d^>action.txt >> input.bat
echo goto loop) >> input.bat
echo if ERRORLEVEL 5 ( >> input.bat
echo echo a^>action.txt >> input.bat
echo goto loop) >> input.bat
echo if ERRORLEVEL 4 ( >> input.bat
echo echo r^>action.txt >> input.bat
echo goto loop) >> input.bat
echo if ERRORLEVEL 3 ( >> input.bat
echo taskkill /f /im cmd.exe >> input.bat
echo exit >> input.bat
echo ) >> input.bat
echo if ERRORLEVEL 2 ( >> input.bat
echo echo s^>action.txt >> input.bat
echo goto loop) >> input.bat
echo if ERRORLEVEL 1 echo w^>action.txt >> input.bat
echo goto loop >> input.bat
)
这是input.bat不存在时的输出(文件input.bat):
This is the output (the file input.bat) when input.bat doesn`t exist:
goto loop
if ERRORLEVEL 5 (
echo a>action.txt
goto loop)
if ERRORLEVEL 4 (
echo r>action.txt
goto loop)
if ERRORLEVEL 3 (
taskkill /f /im cmd.exe
exit
)
if ERRORLEVEL 2 (
echo s>action.txt
goto loop)
if ERRORLEVEL 1 echo w>action.txt
goto loop
推荐答案
(
和)
也必须使用^
进行转义.
(
and )
must be also escaped with ^
.
在命令提示符窗口中输入cmd.exe /?
的最后一页包含特殊字符列表,这些特殊字符在批处理文件中具有特殊含义,因此,如果不在双引号引起来的字符串中,则需要将转义字符解释为原义字符.
The last page on entering cmd.exe /?
in a command prompt window contains the list of special characters which have a special meaning in batch files and therefore require an escape character to be interpreted as literal character if not within a double quoted string.
if not exist input.bat (
echo @echo off > input.bat
echo title Input >> input.bat
echo :set >> input.bat
echo MODE CON: COLS=29 LINES=5 >> input.bat
echo :loop >> input.bat
echo cls >> input.bat
echo echo Gebruik de wasd toetsen >> input.bat
echo echo om te bewegen >> input.bat
echo echo a/Left d/Right >> input.bat
echo choice /c:wscradp /n >> input.bat
echo if ERRORLEVEL 6 ^( >> input.bat
echo echo d^>action.txt >> input.bat
echo goto loop^) >> input.bat
echo if ERRORLEVEL 5 ^( >> input.bat
echo echo a^>action.txt >> input.bat
echo goto loop^) >> input.bat
echo if ERRORLEVEL 4 ^( >> input.bat
echo echo r^>action.txt >> input.bat
echo goto loop^) >> input.bat
echo if ERRORLEVEL 3 ^( >> input.bat
echo taskkill /f /im cmd.exe >> input.bat
echo exit >> input.bat
echo ^) >> input.bat
echo if ERRORLEVEL 2 ^( >> input.bat
echo echo s^>action.txt >> input.bat
echo goto loop^) >> input.bat
echo if ERRORLEVEL 1 echo w^>action.txt >> input.bat
echo goto loop >> input.bat
)
更好的是
if not exist input.bat (
echo @echo off> input.bat
echo title Input>> input.bat
echo :set>> input.bat
echo MODE CON: COLS=29 LINES=^5>> input.bat
echo :loop>> input.bat
echo cls>> input.bat
echo echo Gebruik de wasd toetsen>> input.bat
echo echo om te bewegen>> input.bat
echo echo a/Left d/Right>> input.bat
echo choice /c:wscradp /n>> input.bat
echo if ERRORLEVEL 6 ^(>> input.bat
echo echo d^>action.txt>> input.bat
echo goto loop^)>> input.bat
echo if ERRORLEVEL 5 ^(>> input.bat
echo echo a^>action.txt>> input.bat
echo goto loop^)>> input.bat
echo if ERRORLEVEL 4 ^(>> input.bat
echo echo r^>action.txt>> input.bat
echo goto loop^)>> input.bat
echo if ERRORLEVEL 3 ^(>> input.bat
echo taskkill /f /im cmd.exe>> input.bat
echo exit>> input.bat
echo ^)>> input.bat
echo if ERRORLEVEL 2 ^(>> input.bat
echo echo s^>action.txt>> input.bat
echo goto loop^)>> input.bat
echo if ERRORLEVEL 1 echo w^>action.txt>> input.bat
echo goto loop>> input.bat
)
此版本的>>
左边没有空格,可避免在每行上创建的 input.bat 中尾随空格.为了使该版本适用于所有行,必须对第5行或5>>
中的5
进行转义,而不是将字符5附加到文件中.
This version with no space left of >>
avoids a trailing space in created input.bat on every line. To get this version to work for all lines it is necessary to escape also 5
in line 5 or 5>>
would be interpreted instead of appending character 5 to the file.
这篇关于为什么用echo命令给我的脚本另一个输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!