【问题标题】:Batch file if statement isn't working as expected - syntax errors?批处理文件 if 语句未按预期工作 - 语法错误?
【发布时间】:2016-06-06 22:58:10
【问题描述】:

所以我相信我在尝试编写的批处理文件中存在语法问题。我是编写批处理文件的新手,我仍在学习该语言的工作原理。本质上,我有一个可以使用-run 和其他命令运行的脚本:

ScriptName -run -prereq

我还希望能够为可以使用脚本运行的任何命令提供帮助菜单:

ScriptName -help

后跟更多用户需要帮助的输入参数

** Enter any additional input commands for Script help:

用户输入将类似于:

-prereq -one -two -three

-run 命令应该实际运行并应用这些命令。

-help 命令应该只回显有关请求命令的信息。

只运行没有-run-help 命令的脚本会打印一个非常基本的标题,其中包含-help 存在的版本、时间和解释(如下面的代码所示)

基本上,-run 应该只根据应用的进程调用进程,从标签:process_args 可以看出,同样,-help 将首先采用-help 命令启动,然后它会询问有关更多参数,如 :help_process_args 中所见,它将调用回显信息的标签。

帮助功能似乎有效,但如果我运行没有-run 参数的脚本,它默认为-run 功能。此外,在尝试修复默认行为时,我不断看到语法错误。

谁能明白为什么-help 有效,为什么代码默认为不带参数的-run 行为,而不是打印短标题?此外,是否有任何明显的语法错误? (再次,我是批处理文件的新手)

这里是显示帮助菜单创建和调用运行和帮助进程的相关代码。

echo *********************************************
echo **
echo **     Script Version: x.x
echo **         Started at: %time%
echo **    For options use: ScriptNameHere -help
echo **
echo *********************************************

if /i [%1]==[-help] (
    echo **                 Options:
    echo *********************************************
    echo **
    echo **      -run   = Runs the script
    echo **   -prereq   = Skip prereq steps
    echo ** 
    echo ** Enter any additional input commands for Script help:
    echo **

    goto help_input

    endlocal
    exit /b 0
)
if /i [%1]==[-run] (
    call :process_args %*

    call :labelOne
    call :labelTwo
    call :labelThree

    endlocal
    exit /b 0
)
else (
    goto end
)

:help_input
set input=
set /p input= ** :

goto help_process

:help_process
call :process_help_args %input%
if /i not "%_help_opts:-echo=%"=="%_help_opts%" goto:eof

call :helpLabelOne
call :helpLabelTwo
call :helpLabelThree

:process_help_args
if %errorlevel% neq 0 exit /B %errorlevel%
echo. && echo * Setting Special Arguments
    :help_args_loop
    if not [%1] equ [] (
        if [%_help_opts%] neq [] set _help_opts=%_help_opts%/%1
        if [%_help_opts%] equ [] set _help_opts=%1
        echo -- Found %1
        shift
        goto help_args_loop
    ) else (
        if not defined _help_opts (
            echo * No Options Provided
            set _help_opts=**EMPTY**
        )
    )
goto:eof

:process_args
if %errorlevel% neq 0 exit /B %errorlevel%
echo. && echo * Setting Special Arguments
    :args_loop
    if not [%1] equ [] (
        if [%_opts%] neq [] set _opts=%_opts%/%1
        if [%_opts%] equ [] set _opts=%1
        echo -- Found %1
        shift
        goto args_loop
    ) else (
        if not defined _opts (
            echo * No Options Provided
            set _opts=**EMPTY**
        )
    )
goto:eof

:end
endlocal
echo ** Script Complete at %time%
exit /B 0

【问题讨论】:

  • else 部分必须放在“then”部分的右括号的同一行中(就像您拥有的另外两个 if 一样),否则“else”是作为另一个命令。
  • 我已经尝试过了,但似乎并没有给我不同的结果。我还读到括号周围需要有空格,所以我也在寻找它。
  • 不是答案,而是:LabelOne、:LabelTwo、:LabelThree、:HelpLabelOne 等这些东西在哪里?
  • @Liturgist 他们是调用标签的例子,只是回显信息,所以他们不应该对这个问题有任何意义。

标签: batch-file cmd


【解决方案1】:

变化

    endlocal
    exit /b 0
)
else (
    goto end
)

    endlocal
    exit /b 0
) else (
    goto end
)

产生了以下结果。

C:>labstuff.bat
*********************************************
**
**     Script Version: x.x
**         Started at: 13:17:55.30
**    For options use: ScriptNameHere -help
**
*********************************************
** Script Complete at 13:17:55.32

C:>labstuff.bat -run
*********************************************
**
**     Script Version: x.x
**         Started at: 13:16:15.62
**    For options use: ScriptNameHere -help
**
*********************************************

* Setting Special Arguments
-- Found -run
The system cannot find the batch label specified - labelOne
The system cannot find the batch label specified - labelTwo
The system cannot find the batch label specified - labelThree

【讨论】:

  • 这与@Aacini 在我问题的评论部分提出的建议相同,我回答说它没有给我任何结果。这个问题很可能与 if 语句的结构有关。如果我只是交换帮助的位置并运行 if 语句,那么这些功能都不起作用。据我了解,位置应该不重要,所以肯定有其他东西在破坏它。
  • 我最终弄清楚了,并在下面发布了我的答案。尽管您的回答似乎对我不起作用,但我感谢您的洞察力和努力!
【解决方案2】:

所以……经过无数次的打脸,我想我已经想通了。

基本上,我从 if 语句中取出逻辑并将它们放在标签中,然后在 if 语句中,我指向标签。不知道为什么会这样,但一切正常,所以我会接受它!

echo *********************************************
echo **
echo **     Script Version: x.x
echo **         Started at: %time%
echo **    For options use: ScriptNameHere -help
echo **
echo *********************************************

if /i [%1]==[-help] ( goto end )
if /i [%1]==[-run] ( goto end )
if /i [%1]==[] ( goto end )

:help
echo **                 Options:
echo *********************************************
echo **
echo **      -run   = Runs the script
echo **   -prereq   = Skip prereq steps
echo ** 
echo ** Enter any additional input commands for Script help:
echo **

goto help_input

endlocal
exit /b 0

:run
call :process_args %*

call :labelOne
call :labelTwo
call :labelThree

endlocal
exit /b 0

:help_input
set input=
set /p input= ** :

goto help_process

:help_process
call :process_help_args %input%
if /i not "%_help_opts:-echo=%"=="%_help_opts%" goto:eof

call :helpLabelOne
call :helpLabelTwo
call :helpLabelThree

:process_help_args
if %errorlevel% neq 0 exit /B %errorlevel%
echo. && echo * Setting Special Arguments
    :help_args_loop
    if not [%1] equ [] (
        if [%_help_opts%] neq [] set _help_opts=%_help_opts%/%1
        if [%_help_opts%] equ [] set _help_opts=%1
        echo -- Found %1
        shift
        goto help_args_loop
    ) else (
        if not defined _help_opts (
            echo * No Options Provided
            set _help_opts=**EMPTY**
        )
    )
goto:eof

:process_args
if %errorlevel% neq 0 exit /B %errorlevel%
echo. && echo * Setting Special Arguments
    :args_loop
    if not [%1] equ [] (
        if [%_opts%] neq [] set _opts=%_opts%/%1
        if [%_opts%] equ [] set _opts=%1
        echo -- Found %1
        shift
        goto args_loop
    ) else (
        if not defined _opts (
            echo * No Options Provided
            set _opts=**EMPTY**
        )
    )
goto:eof

:end
endlocal
echo ** Script Complete at %time%
exit /B 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多