Jenkins脚本中的sed命令

8ehkhllq  于 2022-11-21  发布在  Jenkins
关注(0)|答案(1)|浏览(196)

我有一个多行sed命令,它在本地运行的脚本中工作正常,但在Jenkins构建脚本中却出现错误。
这是命令:

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}

我一直收到此错误消息:

+ 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.

我的本地bash版本是GNU bash,版本4.4.20(1)-发行版(x86_64-pc-linux-gnu),sed是版本4.4
在Jenkins上,bash的版本是GNU bash,版本4.2.46(2)-release(x86_64-redhat-linux-gnu),而sed的版本是4.2.2
变量CMAKE_FILE包含一个文件路径,该文件包含如下文本:

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

我正在使用sed更新版本号。
有办法解决吗?

oxiaedzo

oxiaedzo1#

我终于解决了这个问题!因为我有一个旧版本的sed,它不支持扩展的正则表达式,所以我必须执行以下操作来使用基本正则表达式(BRE):

  • 转义组,即左(和右)括号
  • 不转义文字括号
  • 转义+以表示特殊含义
  • 对括号进行转义,即{}

我发现this reference很有用,但它没有提到任何关于组的内容。显然在BRE格式中,组的括号必须转义,而文字括号则不必。
所以这就是我的工作:

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

相关问题