在Jenkins管道脚本中的Shell脚本中创建文件时出现问题

时间:2018-09-27 18:05:32

标签: shell jenkins groovy jenkins-pipeline

我正在尝试使用以下命令在Jenkins管道脚本中创建多行文件。

@Input

不幸的是,我无法创建名为greetings.txt的文件。任何人都可以让我知道我要去哪里了。

Jenkins控制台中的结果:

    sh "echo \"line 1\" >> greetings.txt"
    sh "echo \"line 2\" >> greetings.txt"
    echo "The contents of the file are"
    sh 'cat greetings.text'
    sh 'rm -rf greetings.txt'

任何建议都会有所帮助。

谢谢!

2 个答案:

答案 0 :(得分:0)

未找到名为greetings.text的文件,因为您没有创建文件(猫行扩展名中的拼写错误)。尝试sh 'cat greetings.txt',或者甚至更好地调整脚本:

sh "echo \"line 1\" >> greetings.txt"
sh "echo \"line 2\" >> greetings.txt"
echo "The contents of the file are"
sh 'cat greetings.txt'
sh 'rm -rf greetings.txt'

如果要使用多行命令,还可以使用以下语法:

sh """
echo \"line 1\" >> greetings.txt
echo \"line 2\" >> greetings.txt
echo "The contents of the file are:"
cat greetings.txt
rm -rf greetings.txt
"""

在上一个示例中,这应生成如下输出:

Running shell script
+ echo 'line 1'
+ echo 'line 2'
+ echo 'The contents of the file are:'
The contents of the file are:
+ cat greetings.txt
line 1
line 2
+ rm -rf greetings.txt

答案 1 :(得分:0)

这可以通过在ffmpeg -i image1 -i image2 -filter_complex "[0][1]scale2ref=w='max(iw,main_w)':h=ow/mdar[0max][1ref]; [1ref][0max]scale2ref=w='max(iw,main_w)':h=ow/mdar[1max][0max]; [0max][1max]vstack" -q:v 1 combined.jpg 处使用单引号来解决,因此您无需使用转义。另外,您还必须使用sh创建一个初始文件,并使用>添加内容:

>>

输出:

pipeline{
    agent any

    stages{
        stage('write file'){
            steps{
                sh 'echo "line 1" > greetings.txt'
                sh 'echo "line 2" >> greetings.txt'
                echo "The contents of the file is"
                sh 'cat greetings.txt'
                sh 'rm -rf greetings.txt'
            }
        }
    }
}
相关问题