我建议在双引号中运行bash命令,并转义
$和字符。考虑以下Jenkins管道示例脚本:
#!/usr/bin/env groovypipeline { 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 ipsumdolor sit amet12 13 14test|test
当我运行它时,我得到以下控制台输出:
Started by user admin[Pipeline] nodeRunning on Jenkins in /var/jenkins_home/workspace/test-pipeline[Pipeline] {[Pipeline] stage[Pipeline] { (Build)[Pipeline] echoInital content of temp.txt file[Pipeline] sh[test-pipeline] Running shell script+ cat temp.txtlorem ipsumdolor sit amet12 13 14test|test[Pipeline] sh[test-pipeline] Running shell script+ sed -i :a;N;$!ba;s/n/|#|/g temp.txt[Pipeline] echoContent of temt.txt file after running sed command...[Pipeline] sh[test-pipeline] Running shell script+ cat temp.txtlorem ipsum|#|dolor sit amet|#||#|12 13 14|#||#|test|test|#|[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS运行脚本
temp.txt文件后,将其内容更改为:
lorem ipsum|#|dolor sit amet|#||#|12 13 14|#||#|test|test|#|
希望能帮助到你。



