我正在向(git)克隆存储库编写一个简单的Windows批处理脚本。在其中,我有一个if
语句,该语句不一致地分支:
1 @echo off
2 REM git clone a repo from M:\repo and the checkout the dev branch
3
4 if [%1] EQU [] (
5 echo Usage:
6 echo clone.bat [name_of_repo] [optional: destination_path]
7 goto EXIT
8 )
9
10 set dest_path="."
11 set repo=%1%
12
13 if NOT [%2] EQU [] (
14 set dest_path=%2%
15 )
16
17 if NOT EXIST M:\repo\%repo% (
18 echo Error: repository %repo% does not exist.
19 goto EXIT
20 )
21
22 if NOT EXIST %dest_path% (
23 echo Info: destination %dest_path% does not exist.
24 set /p ans="Create path (Y/n)? "
25 if "%ans%" == "Y" (
26 goto RUN
27 )
28 echo Path not created. Done.
29 goto EXIT
30 )
31
32 :RUN
33 REM test that parameters are set
34 echo %dest_path%
35 echo %repo%
36
37 :EXIT
(我插入了行号以帮助讨论。希望它们不会对您造成影响)
脚本非常简单:
在第24行set /p ans="Create path (Y/n)? "
上,我提示创建路径并将用户的响应设置为变量ans
。
然后,如果响应为“ Y”,则脚本将 1 创建路径,然后转到RUN
,否则应退出。
当我按原样重复运行脚本时,得到以下信息:
bcf@AVA-411962-1 E:\
$ clone gpsrx fake_path
Info: destination fake_path does not exist.
Create path (Y/n)? Y
Path not created. Done.
bcf@AVA-411962-1 E:\
$ clone gpsrx fake_path
Info: destination fake_path does not exist.
Create path (Y/n)? Y
fake_path
gpsrx
bcf@AVA-411962-1 E:\
$
我尝试了if
语句的许多变体。而且,我玩过setlocal EnableDelayedExpansion
/ setlocal DisableDelayedExpansion
废话。
任何人都可以指出问题所在吗?
1 我还没有添加mkpath %dest_path%
(它将在第25行和第25行之间),因为我不希望脚本在执行正常之前实际执行任何操作正确地
答案 0 :(得分:1)
这里有一些代码供您学习?
@Echo Off
Rem git clone a repo from M:\repo and the checkout the dev branch
If "%~1"=="" (
Echo Usage:
Echo %~nx0 [name_of_repo] [optional: destination_path]
Timeout 5 /NoBreak>Nul
Exit /B
)
Set "repo="
If Exist "M:\repo\%~1\" (
Set "repo=%~1"
) Else (
Echo Error: repository %1 does not exist.
Timeout 3 /NoBreak>Nul
Exit /B
)
Set "dest_path="
If Not "%~2"=="" Set "dest_path=%~2"
If Not Exist "%~2\" (
Echo Info: destination %2 does not exist.
Choice /M "Create path"
If ErrorLevel 2 (
Echo Path not created. Done.
Timeout 3 /NoBreak>Nul
) Else MD "%~2"
)
Rem test that parameters are set
Echo=%dest_path%
Echo=%repo%
Pause