为了账号安全,请及时绑定邮箱和手机立即绑定

批处理文件中嵌套的IF有麻烦

批处理文件中嵌套的IF有麻烦

RISEBY 2019-12-06 15:40:49
我正在制作一个批处理文件,用于从SVN中签出项目。我要求用户输入目录,当您到达所需的目录时,键入checkout,它将签出该项目目录。但是,我在下面的代码上遇到了一些麻烦。请帮忙。if /i %choice%==1  ( clssvn ls %svnroot_temp%:topset /p direct=Enter directory:if %direct%=checkout( goto :checkout_area )set svnroot_temp= %svnroot_temp%/%direct%svn ls %svnroot_temp%goto :top)我在哪里错了?
查看完整描述

1 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

从来没有使用:label也没有:: label-like comment封闭在命令块内部()的括号。证明:


@ECHO %1>NUL

if "" == "" (

    @echo a simple echo, no comments

)

if ""=="" (

  @echo a rem comment follows this echo command

  rem comment

  @echo a rem comment precedes this echo command

)

if ""=="" (

  @echo a label-like comment follows this echo command

  :: comment

  @echo a label-like comment precedes this echo command

)

if ""=="" (

  @echo a label follows this echo command

  :label

  @echo a label precedes this echo command

)

输出:


==>D:\bat\labels.bat OFF

a simple echo, no comments

a rem comment follows this echo command

a rem comment precedes this echo command

a label-like comment follows this echo command

'@echo' is not recognized as an internal or external command,

operable program or batch file.

a label follows this echo command

'@echo' is not recognized as an internal or external command,

operable program or batch file.


==>

如果我可以理解您的目标,那么下一个代码段应该可以按预期工作:


SETLOCAL enableextensions


rem (set `svnroot_temp` and `choice` variables here)


if /i "%choice%"=="1"  ( 

    cls

    svn ls %svnroot_temp%

    call :top

)

goto :eof


:top

set /p direct=Enter directory:

if /I "%direct%"=="checkout" goto :checkout_area

set "svnroot_temp=%svnroot_temp%\%direct%"

svn ls %svnroot_temp%

goto :top


:checkout_area

请注意,两个比较表达式if /I "%direct%"=="checkout" goto :checkout_area都用双引号引起来,因为任何用户输入都可能包含空格,甚至可能为空。

不确定要引用svn ls "%svnroot_temp%"。


不确定命令"%svnroot_temp%"的输入目录还是输出目录svn ls:


如果输入:if not exist "%svnroot_temp%\%direct%\" goto :top 在更改之前先检查使用set "svnroot_temp=%svnroot_temp%\%direct%"

万一输出:更改MD "%svnroot_temp%" 2>NUL 后使用创建。


查看完整回答
反对 回复 2019-12-06
  • 1 回答
  • 0 关注
  • 805 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信