批处理程序无故无错退出
Batch program exiting for no reason without error
我正在为我的其他程序制作批处理程序 "Super Command Promt"(在 pastebin 上查看它真的很棒!)而且我注意到为它制作插件有点棘手所以我决定开始工作在一个简单的编译器上使制作插件更容易,但我似乎无法让它工作!我打开文件类型命令:
compile test.txt test.scmd
但它不会工作它只是没有错误地退出而不是创建文件并返回到提示屏幕
Heres the Script:
REM Made By LiamBogur
REM To install copy into notepad and press save as select all files and save it as "SCMDCOMPILER.bat"
REM Feel free to message me or email me at: liamhmccracken@gmail.com (I dont check my email much so try messaging me first)
cls
@echo off
set Version=1.0.0
title Super Command Prompt Plugin Compiler
echo Super Command Prompt Plugin Compiler [Version %Version%]
echo.
:A
set CommandA=
set Command=
set CommandB=
set CommandC=
set /p CommandA=^>
for /f "tokens=1" %%i in ("%CommandA%") do set Command=%%i
for /f "tokens=2" %%i in ("%CommandA%") do set CommandB=%%i
for /f "tokens=3" %%i in ("%CommandA%") do set CommandC=%%i
if /I %Command%==HELP echo Compiler ^| HELP Displays This Message
if /I %Command%==HELP echo Compiler ^| COMPILE [inputfile] [outputfile] Compiles The Input File Into SCMD Format (eg. input.txt output.scmd)
if /I %Command%==HELP echo Compiler ^| EXIT Exits The Compiler
if /I %Command%==HELP echo Commands ^| COMMAND [command] [commands] Makes The Command "command" And Makes It Run "commands"
if /I %Command%==HELP echo Commands ^| RUN [commands] Run Some Commands In Batch
if /I %Command%==HELP echo Commands ^| COMMENT [comment] Adds A Comment To Your File
if /I %Command%==HELP echo Commands ^| HELP [message] Adds That Message To PHELP (eg."HELP Restart Restarts The Computer")
if /I %Command%==EXIT exit
if /I %Command%==COMPILE goto B
goto A
:B
echo Decoding %CommandB%.
for /f "delims=*" %%i in (%CommandB%) do (
set WordN=0
for /f "tokens=*" %%u in ("%%i") do (
set /A WordN+=1
if %WordN%==1 (
set CCommand=0
if /I %%u==COMMAND (
set CCommand=1
set LineO=if ^%A^%==2 if /I ^%Command^%==
)
if /I %%u==RUN (
set CCommand=2
set LineO=call
)
if /I %%u==COMMENT (
set CCommand=3
set LineO=rem
)
if /I %%u==HELP (
set CCommand=4
set LineO=if ^%A^%==2 if /I ^%Command^%==PHELP echo %CommandC:~0,-5% ^^^|
)
) else (
if CCommand==1 (
set LineO=%LineO%%%u
) else (
set LineO=%LineO% %%u
)
)
echo %LineO%>>C:\Users\Public\Temp.txt
)
mv C:\Users\Public\Temp.txt %CommandC%
goto A
我真的需要这方面的帮助!
编辑 1:
这是 test.txt 的代码:
COMMENT Lol this is cool
COMMAND test Lol 123 456
HELP Oh No!
这就是编译后的样子
REM Lol this is cool
if %A%==2 if /I %Command%==test Lol 123 456
if %A%==2 if /I %Command%==PHELP echo TEST ^| Oh No!
确保回显打开后我看到的错误消息是:
echo was unexpected at this time.
这并不完全有启发性,但它确实表明存在语法错误(虽然不是 echo — 可能是它之前的内容)。
封装字符串变量
我在您所做的事情中看到的根本问题是您正在与可能为空的变量进行字符串比较,并且您没有将它们括在引号内。这成为语法错误,因为 cmd 运行的命令看起来像
if /I ==HELP echo Commands ^| HELP [message]
cmd 对变量不聪明。它在引用变量的任何地方进行直接替换。
构建非脆弱性的典型方法(稳健 在应用于任何有关 cmd 的内容时夸大其词)是将 LHS 和 RHS 值都包含在引号中,以便产生空值在语法上合法的陈述中。
if /I "%Command%"=="HELP" echo Commands ^| HELP [message]
这样,当 Command
为空的替换发生时(对于输入的最后一行),执行的命令看起来像这样
if /I ""=="HELP" echo Commands ^| HELP [message]
这在语法上是合法的。
使用 /B 退出
您可能想使用 EXIT /B
而不仅仅是 EXIT
,因为 EXIT
本身会结束调用 cmd 进程,在测试中,这通常是您的 cmd window。大多数人不希望这种情况发生。 EXIT /B
将结束脚本而不结束 shell 进程。
我发现出于某种原因,我的特定程序嵌套 for 循环有点错误,它们只是随机运行并且没有真正起作用,所以我创建了一个函数并将我的第二个 for 循环放在那里然后刚刚调用了函数,然后就完美运行了!
我正在为我的其他程序制作批处理程序 "Super Command Promt"(在 pastebin 上查看它真的很棒!)而且我注意到为它制作插件有点棘手所以我决定开始工作在一个简单的编译器上使制作插件更容易,但我似乎无法让它工作!我打开文件类型命令:
compile test.txt test.scmd
但它不会工作它只是没有错误地退出而不是创建文件并返回到提示屏幕
Heres the Script:
REM Made By LiamBogur
REM To install copy into notepad and press save as select all files and save it as "SCMDCOMPILER.bat"
REM Feel free to message me or email me at: liamhmccracken@gmail.com (I dont check my email much so try messaging me first)
cls
@echo off
set Version=1.0.0
title Super Command Prompt Plugin Compiler
echo Super Command Prompt Plugin Compiler [Version %Version%]
echo.
:A
set CommandA=
set Command=
set CommandB=
set CommandC=
set /p CommandA=^>
for /f "tokens=1" %%i in ("%CommandA%") do set Command=%%i
for /f "tokens=2" %%i in ("%CommandA%") do set CommandB=%%i
for /f "tokens=3" %%i in ("%CommandA%") do set CommandC=%%i
if /I %Command%==HELP echo Compiler ^| HELP Displays This Message
if /I %Command%==HELP echo Compiler ^| COMPILE [inputfile] [outputfile] Compiles The Input File Into SCMD Format (eg. input.txt output.scmd)
if /I %Command%==HELP echo Compiler ^| EXIT Exits The Compiler
if /I %Command%==HELP echo Commands ^| COMMAND [command] [commands] Makes The Command "command" And Makes It Run "commands"
if /I %Command%==HELP echo Commands ^| RUN [commands] Run Some Commands In Batch
if /I %Command%==HELP echo Commands ^| COMMENT [comment] Adds A Comment To Your File
if /I %Command%==HELP echo Commands ^| HELP [message] Adds That Message To PHELP (eg."HELP Restart Restarts The Computer")
if /I %Command%==EXIT exit
if /I %Command%==COMPILE goto B
goto A
:B
echo Decoding %CommandB%.
for /f "delims=*" %%i in (%CommandB%) do (
set WordN=0
for /f "tokens=*" %%u in ("%%i") do (
set /A WordN+=1
if %WordN%==1 (
set CCommand=0
if /I %%u==COMMAND (
set CCommand=1
set LineO=if ^%A^%==2 if /I ^%Command^%==
)
if /I %%u==RUN (
set CCommand=2
set LineO=call
)
if /I %%u==COMMENT (
set CCommand=3
set LineO=rem
)
if /I %%u==HELP (
set CCommand=4
set LineO=if ^%A^%==2 if /I ^%Command^%==PHELP echo %CommandC:~0,-5% ^^^|
)
) else (
if CCommand==1 (
set LineO=%LineO%%%u
) else (
set LineO=%LineO% %%u
)
)
echo %LineO%>>C:\Users\Public\Temp.txt
)
mv C:\Users\Public\Temp.txt %CommandC%
goto A
我真的需要这方面的帮助!
编辑 1: 这是 test.txt 的代码:
COMMENT Lol this is cool
COMMAND test Lol 123 456
HELP Oh No!
这就是编译后的样子
REM Lol this is cool
if %A%==2 if /I %Command%==test Lol 123 456
if %A%==2 if /I %Command%==PHELP echo TEST ^| Oh No!
确保回显打开后我看到的错误消息是:
echo was unexpected at this time.
这并不完全有启发性,但它确实表明存在语法错误(虽然不是 echo — 可能是它之前的内容)。
封装字符串变量
我在您所做的事情中看到的根本问题是您正在与可能为空的变量进行字符串比较,并且您没有将它们括在引号内。这成为语法错误,因为 cmd 运行的命令看起来像
if /I ==HELP echo Commands ^| HELP [message]
cmd 对变量不聪明。它在引用变量的任何地方进行直接替换。
构建非脆弱性的典型方法(稳健 在应用于任何有关 cmd 的内容时夸大其词)是将 LHS 和 RHS 值都包含在引号中,以便产生空值在语法上合法的陈述中。
if /I "%Command%"=="HELP" echo Commands ^| HELP [message]
这样,当 Command
为空的替换发生时(对于输入的最后一行),执行的命令看起来像这样
if /I ""=="HELP" echo Commands ^| HELP [message]
这在语法上是合法的。
使用 /B 退出
您可能想使用 EXIT /B
而不仅仅是 EXIT
,因为 EXIT
本身会结束调用 cmd 进程,在测试中,这通常是您的 cmd window。大多数人不希望这种情况发生。 EXIT /B
将结束脚本而不结束 shell 进程。
我发现出于某种原因,我的特定程序嵌套 for 循环有点错误,它们只是随机运行并且没有真正起作用,所以我创建了一个函数并将我的第二个 for 循环放在那里然后刚刚调用了函数,然后就完美运行了!