Weird behavior of windows CMD when running groovy

后端 未结 2 1152
执念已碎
执念已碎 2021-01-13 02:48

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.

相关标签:
2条回答
  • 2021-01-13 02:49

    Check compilation options of groovy. It might be compiled with wildargs, which would cause the stdlib parser to wildcard-expand the regex.

    0 讨论(0)
  • 2021-01-13 02:53

    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.

    0 讨论(0)
提交回复
热议问题