多行输出回显到txt文件

时间:2019-08-01 08:20:59

标签: bash jenkins-pipeline sh echo

我想用echo将我的最后一次提交写入txt文件。当我用echo检查我的最后一次提交时,系统会显示3行的提交信息,但是当我尝试将这些输出写入txt文件时错误。

我在jenkinsfile上写了那些脚本。这就是为什么我尝试在sh部分中添加#/ bin / bash和-e,但仍然出现相同错误的错误。

jenkinsfile中的脚本:

last_commit = sh(returnStdout: true, script: "git log -1").trim()
echo "${last_commit}"

log = log.plus("Last Commit: ${last_commit}\n\r") 
sh 'echo Last Commit: ${last_commit} >> log.txt'

输出结果:

2019-08-01 11:07:32.261  + git log -1
[Pipeline] echo
2019-08-01 11:07:32.306  commit 546548643naseqw1a77325a0345251465519d6
2019-08-01 11:07:32.307  Author: Jack <jack@sample.com>
2019-08-01 11:07:32.307  Date:   Thu Aug 1 11:07:18 2019 
2019-08-01 11:07:32.307  
[Pipeline] sh
2019-08-01 11:07:32.678  + echo -e Last Commit: commit 546548643naseqw1a77325a0345251465519d6
2019-08-01 11:07:32.678  -e Last Commit: commit 546548643naseqw1a77325a0345251465519d6
2019-08-01 11:07:32.678  /jenkins/workspace/sample_job@tmp/durable-013cf5ad/script.sh: 10: /jenkins/workspace/sample_job@tmp/durable-05235d/script.sh: Syntax error: newline unexpected
[Pipeline] }
[Pipeline] // script
[Pipeline] }

1 个答案:

答案 0 :(得分:0)

对我有用。

 sh """ 
  #!/bin/bash
  set -ex
  echo "Last Commit: ${last_commit}" >> changelog.txt 
  """