从批处理文件 FOR 循环运行 SVN 复制时出现问题

发布于 2024-10-12 06:31:11 字数 945 浏览 3 评论 0原文

我在 .BAT 文件中有一个循环,它执行以下操作,作为让我分支项目的快速技巧(输入检查等省略,这是运行的命令):

@for %%X in (%~3) do svn copy ^
 https://my.svn.account/%%X/trunk ^
 "https://my.svn.account/%%X/branches/%~1" ^
 -m "%~2"

所以你可以像这样使用它:

branchTool test_branch "测试分支工具" "proj1,proj2"

SVN 命令看起来正确:

>branchTool.bat test_branch "testing branch tool" "proj1,proj2"
svn copy  https://my.svn.account/proj1/trunk  "https://my.svn.account/proj1/branches/test_branch"  -m "testing branch tool"
svn copy  https://my.svn.account/proj2/trunk  "https://my.svn.account/proj2/branches/test_branch"  -m "testing branch tool"

但是 SVN 并未实际运行 - .bat 文件只是循环快速打印命令,并且没有任何反应。

如果我将写入控制台的每一行的输出复制粘贴并单独运行它们,它们将按预期工作

关于使用丑陋的 DOS 黑客脚本的评论,有什么错误?即使输入一个项目名称也会失败,例如运行:

branchTool test_branch "测试分支 工具”proj1

I have a loop in a .BAT file which does the following, as a quick hack to let me branch projects (input checking, etc left out, this is the command which gets run):

@for %%X in (%~3) do svn copy ^
 https://my.svn.account/%%X/trunk ^
 "https://my.svn.account/%%X/branches/%~1" ^
 -m "%~2"

So you use it like:

branchTool test_branch "testing branch tool" "proj1,proj2"

The SVN commands look right:

>branchTool.bat test_branch "testing branch tool" "proj1,proj2"
svn copy  https://my.svn.account/proj1/trunk  "https://my.svn.account/proj1/branches/test_branch"  -m "testing branch tool"
svn copy  https://my.svn.account/proj2/trunk  "https://my.svn.account/proj2/branches/test_branch"  -m "testing branch tool"

However SVN isn't actually running - the .bat file just loops through quickly printing the commands, and nothing happens.

If I copy-paste the output from each line written to the console and run them individually, they work as expected

Comments on using ugly DOS hacking scripts aside, what's the bug? It fails the same with even one input project name, e.g running:

branchTool test_branch "testing branch
tool" proj1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜灵血窟げ 2024-10-19 06:31:11

我假设这与您使用插入符字符有关。我一直避免使用它 - 但不记得原因......

我建议尝试使用大括号。例如:

@for %%X in (%~3) do (
    svn copy https://my.svn.account/%%X/trunk "https://my.svn.account/%%X/branches/%~1" -m "%~2"
)

您还可以将命令分开以获得更好的易读性/可维护性。我一直更喜欢将编号参数分配给命名环境变量。然后,您可以使参数的意图对于脚本的后续读者来说显而易见。 (我通常将任何环境变量的使用包装在 startlocal/endlocal 对中,以确保我不会用临时变量污染调用者环境。例如:

setlocal
for %%X in (%~3) do (
  set SRC=https://my.svn.account/%%X/trunk
  set DST=https://my.svn.account/%%X/branches/%~1
  svn copy "%SRC%" "%DST" -m "%~2"
)
endlocal

I'm assuming its something to do with your use of the caret character. I've always avoided using it - but can't remember the reason why ....

I'd suggest trying braces instead. E.g.:

@for %%X in (%~3) do (
    svn copy https://my.svn.account/%%X/trunk "https://my.svn.account/%%X/branches/%~1" -m "%~2"
)

You could also break the command apart for better legiblity/maintainabilty. I've always prefered assigning numbered parameters to named environment variables. You can then make the intent of the parameters obvious to a later reader of your script. (I usually wrap any environment variable usage in a startlocal/endlocal pair to ensure I don't pollute the callers environment with my temporary variables. E.g.:

setlocal
for %%X in (%~3) do (
  set SRC=https://my.svn.account/%%X/trunk
  set DST=https://my.svn.account/%%X/branches/%~1
  svn copy "%SRC%" "%DST" -m "%~2"
)
endlocal
心凉 2024-10-19 06:31:11

您可以尝试指定 svn 可执行文件的完全限定路径,例如 C:\path\to\Subversion\svn.exe。当您执行批处理脚本时,svn 可执行文件可能不在环境的路径上。

You might try specifying the fully qualified path to the svn executable, for example C:\path\to\Subversion\svn.exe. It could be that the svn executable is not on environment's path when you execute the batch script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文