本地正常的多行sed命令在Jenkins脚本中报错,求解决方案
Jenkins中sed版本兼容问题修复
问题背景
本地运行正常的多行sed命令,在Jenkins构建脚本中执行报错,提示sed使用方法错误。
环境差异
- 本地:GNU bash 4.4.20、sed 4.4
- Jenkins:GNU bash 4.2.46、sed 4.2.2
原错误命令
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.
目标文件示例(CMakeLists.txt)
# 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 )
错误原因
GNU sed 4.2.2(Jenkins环境)不支持多次使用-i选项,而本地的sed 4.4允许重复使用全局选项。重复的-i会被旧版本sed判定为无效参数,触发使用方法报错。
修复后的命令
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} \)/" \ -e "s/(set\(.* APPMANAGER_MAJOR_VERSION\s+)[0-9]{1,2}\s*\)/\1${RELEASE_MAJOR} \)/" \ -e "s/(set\(.* APPMANAGER_MINOR_VERSION\s+)[0-9]{1,2}\s*\)/\1${RELEASE_MINOR} \)/" \ -e "s/(set\(.* APPMANAGER_MICRO_VERSION\s+)[0-9]{1,2}\s*\)/\1${RELEASE_MICRO} \)/" \ "${CMAKE_FILE}"
优化说明
- 合并全局选项:只保留一次
-i(原地编辑)和-r(扩展正则)选项,避免旧版本sed无法识别重复选项。 - 变量引用规范:给版本变量加上大括号
${RELEASE_MAJOR},防止变量名与后续字符意外拼接,提升代码鲁棒性。 - 路径加引号:给
${CMAKE_FILE}包裹双引号,避免文件路径包含空格时出现参数拆分错误。
内容的提问来源于stack exchange,提问作者jignatius
相关产品推荐
相关产品推荐

