简体   繁体   English

运行时出现批处理错误

[英]Get A Batch Error When Running

当我运行此代码时,我收到错误,但后来我看不到它,因为批处理文件快速关闭。有人知道怎么修这个东西吗?

When I run this all I can see before it closes are a few lines eg _ and a black batch file screen closing. 当我运行这个我可以看到它关闭之前的几行,例如_和黑色批处理文件屏幕关闭。

The | | character is called a pipe. 字符称为管道。 It separates commands, pushing the output of one command into the input of another. 它将命令分开,将一个命令的输出推送到另一个命令的输入。 Because they have special meaning in batch scripts, you can't just echo them without escaping them with a caret, or setting them to a variable and retrieving with delayed expansion. 因为它们在批处理脚本中具有特殊含义,所以您不能仅使用插入符号回显它们,或者将它们设置为变量并使用延迟扩展进行检索。

Here's a simple example of escaping: 这是一个简单的转义示例:

@echo off
setlocal

echo                      _______             _____    _______              _______
echo \                /  ^|          ^|        /     \  /       \  ^|\    /^|  ^|
echo  \              /   ^|          ^|        ^|        ^|       ^|  ^| \  / ^|  ^|
echo   \            /    ^|_______   ^|        ^|        ^|       ^|  ^|  \/  ^|  ^|_______
echo    \    /\    /     ^|          ^|        ^|        ^|       ^|  ^|      ^|  ^|
echo     \  /  \  /      ^|          ^|        ^|        ^|       ^|  ^|      ^|  ^|
echo      \/    \/       ^|_______   ^|______  \_____/  \_______/  ^|      ^|  ^|_______
pause

Here's an example with delayed expansion: 这是延迟扩展的示例:

@echo off
setlocal enabledelayedexpansion
set "I=|"

echo                      _______             _____    _______              _______
echo \                /  !I!          !I!        /     \  /       \  !I!\    /!I!  !I!
echo  \              /   !I!          !I!        !I!        !I!       !I!  !I! \  / !I!  !I!
echo   \            /    !I!_______   !I!        !I!        !I!       !I!  !I!  \/  !I!  !I!_______
echo    \    /\    /     !I!          !I!        !I!        !I!       !I!  !I!      !I!  !I!
echo     \  /  \  /      !I!          !I!        !I!        !I!       !I!  !I!      !I!  !I!
echo      \/    \/       !I!_______   !I!______  \_____/  \_______/  !I!      !I!  !I!_______
pause

As an advanced exercise, if you'd like to keep the figlet "WELCOME" text readable within the source code, you could employ a batch script heredoc : 作为一项高级练习,如果您想在源代码中保持figlet“WELCOME”文本可读,您可以使用批处理脚本heredoc

@echo off
setlocal

call :heredoc welcome && goto end_welcome
                     _______             _____    _______              _______
\                /  |          |        /     \  /       \  |\    /|  |
 \              /   |          |        |        |       |  | \  / |  |
  \            /    |_______   |        |        |       |  |  \/  |  |_______
   \    /\    /     |          |        |        |       |  |      |  |
    \  /  \  /      |          |        |        |       |  |      |  |
     \/    \/       |_______   |______  \_____/  \_______/  |      |  |_______
:end_welcome
pause

goto :EOF

rem // https://stackoverflow.com/a/15032476/1683264
:heredoc <uniqueIDX>
setlocal enabledelayedexpansion
set go=
for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
    set "line=%%A" && set "line=!line:*:=!"
    if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
    if "!line:~0,13!"=="call :heredoc" (
        for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
            if #%%i==#%1 (
                for /f "tokens=2 delims=&" %%I in ("!line!") do (
                    for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
                )
            )
        )
    )
)
goto :EOF

By the way, you should keep in mind that command prompt windows are 80 columns by default. 顺便说一句,您应该记住,命令提示符窗口默认为80列。 What you have screenshot looks like it'll be too wide for a typical console window. 您拥有的屏幕截图看起来对于典型的控制台窗口来说太宽了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM