简体   繁体   English

意外的标记 '。' 在 groovy 脚本中更改文件扩展名时执行 shell 命令时

[英]unexpected token '.' when executing a shell command when changing file extension within groovy script

I'm trying to run the following command within a.groovy script:我正在尝试在 a.groovy 脚本中运行以下命令:

sh """#!/bin/bash -ex
    find .. -name "*.gcda" | \
    xargs -I {} sh -c 'file=${0}; echo "${file%.*}".gcno' {} | \
    xargs -I {} sh -c 'gcov {} -o "\$(dirname {})"' \\;
"""

Not the prettiest command, but I'm finding a list of .gcda file names, changing those strings to have .gcno extensions instead, and then finally running a command on the .gcno version of the filename.不是最漂亮的命令,但我正在查找.gcda文件名列表,将这些字符串更改为具有.gcno扩展名,然后最终在文件名的.gcno版本上运行命令。

The error I get:我得到的错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 92: unexpected token: . @ line 92, column 60.
   h -c 'file=${0}; echo "${file%.*}".gcno'
                                 ^

1 error

This command works fine when I run it in the terminal, it just complains when I execute it in Jenkins.当我在终端中运行这个命令时它运行良好,当我在 Jenkins 中执行它时它只是抱怨。 I tried escaping a few things but couldn't figure it out.我尝试了 escaping 一些东西,但无法弄清楚。

Here's an easier way to reproduce your problem:这是重现问题的更简单方法:

unexpected token: . @ line 1, column 14.
   """ echo ${1%.} """
                ^

This happens because ${..} in Groovy double quoted strings are used for string interpolation.发生这种情况是因为 Groovy 双引号字符串中的${..}用于字符串插值。 You instead want Groovy to leave them alone so that the shell can interpret them.相反,您希望 Groovy 不理会它们,以便 shell 可以解释它们。

You can do this with triple single quoted, since Groovy does not do string interpolation in those (but unlike bash itself, it still interprets backslashes):您可以使用三重单引号来执行此操作,因为 Groovy 不会在其中进行字符串插值(但与 bash 本身不同,它仍然解释反斜杠):

sh '''#!/bin/bash -ex
    find .. -name "*.gcda" | \
    xargs -I {} sh -c 'file=${0}; echo "${file%.*}".gcno' {} | \
    xargs -I {} sh -c 'gcov {} -o "$(dirname {})"' \\;
'''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM