[英]Running sh commands in Jenkins Pipeline with escaping quotes
I am running a shell script within the Jenkins pipeline and i want to print into a text file latest files names column created by date, i dont want want to print time & date column etc, just anything after 9 which are files names and print to into a txt file.. 我在Jenkins管道中运行Shell脚本,我想将由日期创建的最新文件名列打印到文本文件中,我不想打印时间和日期列等,仅9位之后的内容即文件名并打印到到txt文件中。
codes are as below: 代码如下:
Note: when i run those commands in bash all work fine, \\ is added as Jenkins does not seems to like the $ without the dollar sign all run fine in shell. 注意:当我在bash中运行这些命令都可以正常工作时,会添加\\,因为Jenkins似乎不喜欢没有美元符号的$在shell中都可以正常运行。
out put in shell is like this: 放在外壳中是这样的:
bash.sh
home
testfile.txt
blabla.csv
rtyuioiuytrty.xml
like above I would like to be printed in Jenkinsfile. 像上面一样,我想打印在Jenkinsfile中。 Jenkins does not seem to like to run those codes as below: I also tried this loop Jenkins似乎不喜欢按以下方式运行这些代码:我也尝试过此循环
node ('node') {
stage ('SSH To server') {
sshagent(credentials: ['sshkey']) {
script {
sh"""ssh -o StrictHostKeyChecking=no user@servername << EOF
if [ \$? -ne 0 ]; then
echo " Error while connecting SSH "
exit 1
fi
cd ${SOURCE_PATH}
if [ \$? -ne 0 ]; then
echo "Error while doing change directory \${SOURCE_PATH} "
exit 1
fi
ls -lrt | grep "$(date '+%b %e')" |awk '{ s =""; for (i = 9; i <= NF; i++) s = s $i " "; print }'
exit
EOF
"""
Welcome to the hell of escaping in Jenkins :-) 欢迎来到逃脱詹金斯的地狱:-)
probably the below command will help you 下面的命令可能会帮助您
node(''){
sh "touch test.txt"
sh """ls -lrt | grep \"\$(date '+%b %e')\" |awk '{ s =\"\"; for (i = 9; i <= NF; i++) s = s \$i \" \"; print s }'"""
}
The gist is, you will have to escape double quotes and the dollar. 要点是,您将必须避免使用双引号和美元。
Answer for your updated code snippet is 您更新后的代码段的答案是
sh"""ssh -o StrictHostKeyChecking=no user@servername << EOF
if [ \$? -ne 0 ]; then
echo \" Error while connecting SSH \"
exit 1
fi
cd \${SOURCE_PATH}
if [ \$? -ne 0 ]; then
echo \"Error while doing change directory \${SOURCE_PATH} \"
exit 1
fi
ls -lrt | grep \"\$(date '+%b %e')\" | awk '{ print \\\$9}'
exit
EOF
"""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.