运行groovy时windows CMD的奇怪行为 [英] Weird behavior of windows CMD when running groovy

查看:675
本文介绍了运行groovy时windows CMD的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个groovy脚本,重命名匹配正则表达式的文件
我以这种方式启动它

  C: \> groovy rename test。* test.txt 

它可以正常工作。



但是当我尝试传递这个正则表达式时:

  C:\> groovy重命名test \。(。*)$ 1_TEST_ $ 1 

命令行返回一条消息:



该命令的语法不正确



不是我的脚本 - 它的 cmd 写这个。我的脚本从未运行。即使将参数(正则表达式)换成引号也会发生。



问:为什么?我怎样才能将任何正则表达式作为参数传递?





由于没有人能弄清楚发生了什么,我做了一个更简单的例子:


test.groovy
$ b

  println args [0] 

我运行它: groovy test Bob ,输出结果并不奇怪, Bob
但是当我运行例如 groovy test。* 我得到命令的语法不正确。当我运行 groovy test * 时,会出现一个pdf文件的名称(恰好与 test.groovy

解决方案

好的,这似乎是 startgroovy.bat中的一个错误批处理文件。错误发生在这里:

  rem删除前导空间,我们将首次添加
,如果x%_ARG :〜0,1%==xset _ARG =%_ ARG:〜1%

但问题在于以下几行:

  rem删除第一个参数周围的引号
为%% i in(% 1)do set _ARG =%_ ARG %%%〜i

这不符合预期该参数包含通配符。某些情况(包括单个星号)在前面的脚本和其他类型的工作中都有效,因为通配符匹配成功;在这种情况下,您不会收到错误,但匹配的文件将传递给Groovy脚本而不是通配符。也有一些情况下通配符会匹配,但并不是因为解决方法已经改变了它。



就我所知,目的是不处理通配符,所以修正很简单:

  rem删除第一个参数周围的引号
for / F %% i in(%1 )do set _ARG =%_ ARG %%%〜i

现在它适用于我:

  H:\> groovy test。* 
。*
pre>

我打算提交一个错误报告,但看起来好像我不能在没有首先在项目网站上注册的情况下这样做,所以您可以提交一个如果你喜欢的话。



附注:

虽然问题是由这个bug在 startgroovy.bat 中也暴露了Windows批处理器中的一个bug(或者至少是一个奇怪的行为)。如果变量不存在,环境变量子字符串扩展的行为会很奇怪。上面显示的 if 行在命令行上按预期工作(没有环境变量替换),但是在批处理文件中,它会被改为:

 如果x〜0,1_ARG:〜1 

因此语法错误。

I have a groovy script that renames files that match a regex I launch it this way

C:\>groovy rename test.* test.txt

It works fine.

But when I try to pass this regex:

C:\>groovy rename test\.(.*) $1_TEST_$1

the command line returns a message:

The syntax of the command is incorrect

and it's not my script - its cmd that writes this. My script never even runs. It also happens even when I wrap the arguments (regex) in quotes.

Q: Why? How can I pass any regex as param?


Since nobody can figure out what's going on, I've made an even simpler example:

test.groovy

println args[0]

I run it: groovy test Bob and the output is, not suprisingly, Bob. But when I run e.g. groovy test .* I get The syntax of the command is incorrect. When I run it groovy test * then a name of a pdf file comes out (that happens to be in the same dir as test.groovy)

解决方案

OK, this appears to be a bug in the startgroovy.bat batch file. The error occurs here:

rem remove the leading space we'll add the first time
if "x%_ARG:~0,1%" == "x " set _ARG=%_ARG:~1%

But the problem is a few lines previously:

rem remove quotes around first arg
for %%i in (%1) do set _ARG=%_ARG% %%~i

This doesn't work as intended if the argument contains wildcards. Certain cases (including a single asterisk) are worked around earlier in the script and others sort of work because the wildcard matches successfully; in this case you don't get an error but the matching file is passed to the Groovy script rather than the wildcard. There are also cases where the wildcard would match but doesn't because the workaround has mangled it.

As near as I can figure, the intent is to not process wildcards, so the fix is straightforward:

rem remove quotes around first arg
for /F %%i in (%1) do set _ARG=%_ARG% %%~i

Now it works for me:

H:\>groovy test .*
.*

I was intending to submit a bug report, but it looks as if I can't do so without first registering with the project web site, so you can submit one if you like.

Side note:

Although the problem is caused by the bug in startgroovy.bat it also exposes a bug (or, at least, an odd behaviour) in the Windows batch processor; environment variable substring expansion behaves oddly if the variable doesn't exist. The if line shown above works as expected (no environment variable substitution) on the command line, but in a batch file it gets mangled, coming out as:

if "x~0,1_ARG:~1

Hence the syntax error.

这篇关于运行groovy时windows CMD的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆