简体   繁体   English

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

[英]Issues while create a file in shell scripting in jenkins pipeline script

I am trying to create a multi line file in Jenkins pipeline script using the below commands. 我正在尝试使用以下命令在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'

Unforunately , I am not able to create the file named greetings.txt. 不幸的是,我无法创建名为greetings.txt的文件。 Can any one please let me know, where I am going wrong. 任何人都可以让我知道我要去哪里了。

Results in Jenkins console: Jenkins控制台中的结果:

[tagging] Running shell script
+ echo 'line 1'
[Pipeline] sh
[tagging] Running shell script
+ echo 'line 2'
[Pipeline] echo
The contents of the file are
[Pipeline] sh
[tagging] Running shell script
+ cat greetings.text
cat: greetings.text: No such file or directory

Any suggestions would be helpful. 任何的意见都将会有帮助。

Thanks! 谢谢!

It's not finding a file named greetings.text because you didn't create one (a litte typo in the extension in your cat line). 它没有找到一个名为greetings.text的文件,因为您没有创建一个文件(cat行扩展名中的拼写错误)。 Try sh 'cat greetings.txt' , or even better adjusted your script: 尝试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'

If you want to use multiline commands, you can also use this syntax: 如果要使用多行命令,还可以使用以下语法:

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
"""

From the last example, this should generate an output like: 在上一个示例中,这应生成类似以下的输出:

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

This can be solve this by using single quotes with sh , so you don't need to use escaping. 这可以通过在sh使用单引号来解决,因此您无需使用转义。 Also you have to create an initial file with > and add content with >> : 另外,您还必须使用>创建初始文件,并使用>>添加内容:

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'
            }
        }
    }
}

output: 输出:

[test] Running shell script
+ echo line 1
[Pipeline] sh
[test] Running shell script
+ echo line 2
[Pipeline] echo
The contents of the file is
[Pipeline] sh
[test] Running shell script
+ cat greetings.txt
line 1
line 2
[Pipeline] sh
[test] Running shell script
+ rm -rf greetings.txt
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

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

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