一尘不染

詹金斯管道中的sed

jenkins

我正在尝试在詹金斯(Jenkins)中运行以下内容,但我得到任何建议的错误?

     sh ''' sed -i \':a;N;$!ba;s/\\n/\\|\\#\\|/g\' ${concl} '''

错误-为什么${concl}在shell脚本中不使用文件名重新填充?

   + sed -i ':a;N;$!ba;s/\n/\|\#\|/g'
    sed: no input files

阅读 262

收藏
2020-07-25

共1个答案

一尘不染

我建议在双引号中运行bash命令,并转义$\字符。考虑以下Jenkins管道示例脚本:

#!/usr/bin/env groovy

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Inital content of temp.txt file'
                sh 'cat temp.txt'

                sh "sed -i ':a;N;\$!ba;s/\\n/\\|\\#\\|/g' temp.txt"

                echo 'Content of temt.txt file after running sed command...'
                sh 'cat temp.txt'
            }
        }
    }
}

temp.txt我在此示例中使用的文件包含:

lorem ipsum
dolor sit amet

12 13 14

test|test

当我运行它时,我得到以下控制台输出:

Started by user admin
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Inital content of temp.txt file
[Pipeline] sh
[test-pipeline] Running shell script
+ cat temp.txt
lorem ipsum
dolor sit amet

12 13 14

test|test

[Pipeline] sh
[test-pipeline] Running shell script
+ sed -i :a;N;$!ba;s/\n/\|\#\|/g temp.txt
[Pipeline] echo
Content of temt.txt file after running sed command...
[Pipeline] sh
[test-pipeline] Running shell script
+ cat temp.txt
lorem ipsum|#|dolor sit amet|#||#|12 13 14|#||#|test|test|#|
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

运行脚本temp.txt文件后,将其内容更改为:

lorem ipsum|#|dolor sit amet|#||#|12 13 14|#||#|test|test|#|

希望能帮助到你。

2020-07-25