如何运行 ssh /bin/bash << EOF commands in groovy?

问题描述 投票:0回答:1

我在 bash shell 中输入了以下代码:

export letters="a b c"
ssh [email protected] /bin/bash << EOF
echo $letters
for letter in ${letters}; do
    echo "hello \$letter"
done
EOF

并得到了预期的输出

a b c
hello a
hello b
hello c

但是,当我编写一个常规脚本来执行相同操作时:

pipeline {
    stages {
        stage('print letters') {
            steps {
                script {
                env.letters="a b c"
                sh'''
                    ssh [email protected] /bin/bash << EOF
                    echo $letters
                    for letter in ${letters}; do
                        echo "hello \$letter"
                    done
EOF
'''
                }
            }
        }
    }
}

我明白了

a b c
hello 
hello 
hello 

因此,在 ssh shell 中设置的所有变量都将被忽略。

jenkins groovy jenkins-pipeline jenkins-groovy
1个回答
0
投票

Groovy 字符串语法吞掉了一级

\
转义。您需要转义
\
才能将其传递给 shell。

                        echo "hello \\$letter"
© www.soinside.com 2019 - 2024. All rights reserved.