Home > database >  sed command in Jenkins script
sed command in Jenkins script

Time:11-20

I have a multiline sed command that works fine in a script that I run locally, but in a Jenkins build script gives me an error.

This is the command:

sed \
 -i -r -e "s/(project\(.* VERSION\s )[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}\s*\)/\1$RELEASE_MAJOR\.$RELEASE_MINOR\.$RELEASE_MICRO \)/" \
 -i -r -e "s/(set\(.* APPMANAGER_MAJOR_VERSION\s )[0-9]{1,2}\s*\)/\1$RELEASE_MAJOR \)/" \
 -i -r -e "s/(set\(.* APPMANAGER_MINOR_VERSION\s )[0-9]{1,2}\s*\)/\1$RELEASE_MINOR \)/" \
 -i -r -e "s/(set\(.* APPMANAGER_MICRO_VERSION\s )[0-9]{1,2}\s*\)/\1$RELEASE_MICRO \)/" \
 ${CMAKE_FILE}

I keep getting this error message:

  sed -i -r -e 's/(project\(.* VERSION\s )[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}\s*\)/\114\.0\.0 \)/' -i -r -e 's/(set\(.* APPMANAGER_MAJOR_VERSION\s )[0-9]{1,2}\s*\)/\114 \)/' -i -r -e 's/(set\(.* APPMANAGER_MINOR_VERSION\s )[0-9]{1,2}\s*\)/\10 \)/' -i -r -e 's/(set\(.* APPMANAGER_MICRO_VERSION\s )[0-9]{1,2}\s*\)/\10 \)/' /apps/artefacts/jenkins_workspace/AI/Release/RDK-AI-Branch_OFF/asappsserviced/asappsserviced/appinfrastructure/RDK/AppManager/CMakeLists.txt
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
  -b, --binary
                 does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
                 open files in binary mode (CR LFs are not treated specially))
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
  --help
                 display this help and exit
  --version
                 output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

My local bash version is GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu) and sed is version 4.4.

On Jenkins the version of bash is GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) and sed is version 4.2.2.

The variable CMAKE_FILE contains a path to a file containing text like this:

# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required( VERSION 3.10.0 )

# Project setup
project( MyApp LANGUAGES C CXX VERSION 14.0.0 )


# Set the major and minor version numbers (also used by plugins)
set( APPMANAGER_MAJOR_VERSION 14 )
set( APPMANAGER_MINOR_VERSION 0 )
set( APPMANAGER_MICRO_VERSION 0 )

I am using sed to update the version numbers.

Any idea how to fix this?

CodePudding user response:

I finally worked it out! Since I had an old version of sed that didn't support extended regexes, I had to do the following to use basic regular expressions (BRE):

  • escape groups, i.e. the open ( and close ) brackets
  • un-escape literal brackets
  • escape for special meaning
  • escape the parentheses, i.e. { and }

I found this reference useful but it didn't mention anything about groups. Apparently in the BRE format, brackets for groups have to be escaped while literal brackets do not.

So this is what worked for me:

sed \
    -i -e "s/\(project.*VERSION\)\s\ [0-9]\{1,2\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}\s\ )/\1 $major\.$minor\.$micro\ )/" \
    -i -e "s/\(set.*APPMANAGER_MAJOR_VERSION\)\s\ [0-9]\{1,2\}\s*)/\1 $major )/" \
    -i -e "s/\(set.*APPMANAGER_MINOR_VERSION\)\s\ [0-9]\{1,2\}\s*)/\1 $minor )/" \
    -i -e "s/\(set.*APPMANAGER_MICRO_VERSION\)\s\ [0-9]\{1,2\}\s*)/\1 $micro )/" ${FILE}
  • Related