简体   繁体   中英

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:

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.

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. I tried escaping a few things but couldn't figure it out.

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. You instead want Groovy to leave them alone so that the shell can interpret them.

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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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