使用shell cmd重命名jenkins中的目录

时间:2018-07-26 16:59:03

标签: shell jenkins jenkins-pipeline

我正在尝试在jenkins中运行shell脚本来重命名目录以在其中添加日期。

// rename file
sh("mv $file-reports $file-reports-$date")
sh("mv $file-reports-$date jmeter-tests")

日期由下一个脚本获取:

    // Getting date
    date = sh(
        script: """
                    (date +%T-"%F")
                    """,
        returnStdout: true
    )

日期输出: 12:55:39-2018-07-26

我在日志中收到此错误:

[Pipeline] sh
[workspace] Running shell script
+ mv quickquote-belair-appstatic-reports quickquote-belair-appstatic-reports-15:59:27-2018-07-26a
[Pipeline] sh
[workspace] Running shell script
+ mv -T quickquote-belair-appstatic-reports-15:59:27-2018-07-26a
mv: missing destination file operand after ‘quickquote-belair-appstatic-reports-15:59:27-2018-07-26a’
Try 'mv --help' for more information.

我很困惑为什么它告诉我目标文件丢失了..?

1 个答案:

答案 0 :(得分:0)

默认情况下,返回值sh()后面将换行,因此您需要使用trim()删除末尾的新行,如下所示:

date = sh(
        script: """
                    (date +%T-"%F")
                    """,
        returnStdout: true
    ).trim()

由于$date在末尾有换行符,因此mv $file-reports-$date jmeter-tests将在mv $file-reports-$date处断开,目标jmeter-tests将移至下一行。因此它报告mv miss destination file