简体   繁体   English

Jenkinsfile 和 groovy 脚本 - 文件意外结束

[英]Jenkinsfile and groovy script - Unexpected End of File

I need to have a one-liner sh script such as this我需要像这样的单行 sh 脚本

if. [-s filename.txt ] then echo 'don'\''t do something' exit 2 fi

but when I put this in sh " <>" I am getting the following error但是当我把它放在sh " <>"中时,我收到以下错误

/home/jenkins/workspace/<BLA_BLA>/script.sh: line 1: unexpected EOF while looking for matching `''

Not sure how to fix it.不知道如何解决它。 Could someone point me in the right direction?有人能指出我正确的方向吗?

The first issue is that you need to escape the backslash inside the sh block, so for example:第一个问题是您需要转义sh块内的反斜杠,例如:

sh "if. [-s filename.txt ] then echo 'don'\\''t do something' exit 2 fi"

Then there is probably a problem with the oneliner script you posted as it doesn't work in plain bash for me either.那么您发布的 oneliner 脚本可能有问题,因为它对我来说在普通 bash 中也不起作用。

In the pipeline, you can use multiline string with """ , if I reformat the script you posted, it works correctly:在管道中,您可以将多行字符串与"""一起使用,如果我重新格式化您发布的脚本,它可以正常工作:

sh """
    if ! [ -s filename.txt ]; then
        echo 'don'\\''t do something'
        exit 2
    fi
"""

the output then is: output 则为:

+ '[' -s filename.txt ']'
+ echo 'don'\''t do something'
don't do something
+ exit 2
ERROR: script returned exit code 2

Would that work for you?那对你有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM