案例1:在Jenkins管道中运行以下shell脚本时:
pipeline
{
agent any
stages
{
stage('image')
{
steps {
script {
sh ( returnStdout: true,
script: ''' #!/bin/bash
if [[ 56 > 10 ]]
then
echo 'The variable is greater than 10.'
fi
'''
) }
}
}
}
}以上抛出一个异常:
/var/lib/jenkins/workspace/test-job@tmp/durable-f9ee86ef/script.sh: 2: [[: not foundCASE2:但是下面的管道运行得非常好:
pipeline
{
agent any
stages
{
stage('image')
{
steps {
sh '''#!/bin/bash
VAR=56
if [[ $VAR -gt 10 ]]
then
echo "The variable is greater than 10."
fi
'''
}
}
}
}请向我解释为什么相同的shell脚本在上面的CASE2中工作,而它在CASE1中失败?
发布于 2022-01-27 21:45:54
第一个脚本以
script: ''' #!/bin/bash请注意,'''和#之间有一个空格。只有当脚本的前两个字节为#!时,此模式才被识别并定义要使用的shell。如果它是其他任何东西,包括<space>#!,则不会识别它,并使用默认的shell。除非默认的shell是bash,否则[[是无效的。在POSIX中,只有[是有效的。
第二个脚本正确工作的原因是因为'''和#之间没有空格。
https://stackoverflow.com/questions/70883124
复制相似问题