【问题标题】:unexpected EOF while looking for matching寻找匹配时意外的 EOF
【发布时间】:2021-07-22 02:03:20
【问题描述】:

我有 jenkins 作业来执行 shell 脚本,但在寻找匹配项时收到错误意外 EOF

 servicedir="dir1"
 checkdir = sh(
 script: "ssh user@hostname bash -c \"' find /home -name \"${servicedir}\" | wc -l '\"",
 returnStdout: true
 )

返回以下错误

bash -c ''\'' find /home -name dir1'
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file

【问题讨论】:

    标签: linux shell jenkins jenkins-groovy


    【解决方案1】:

    你应该改用三引号,这样你就不会被错误地转义的双引号绊倒:

    脚本:"""ssh user@hostname bash -c 'find /home -name "${servicedir}" | wc -l'""",

    如果你愿意,你也可以删除 wc -l 部分,只计算 Groovy 中的行数:

    // 在远程机器上运行 find 并获取它的标准输出 检查目录 = sh( 脚本:"""ssh user@hostname bash -c 'find /home -name "${servicedir}"'""", 返回标准输出:真 ) // 计算 find 输出的行数 int num_lines = checkdir.count('\n')

    【讨论】:

    • 谢谢@Shane,但是你知道为什么运行shell命令后日志结果分成两行,所以没有找到结果应该是+ ssh user@hostname bash -c 'find /home/ -name "dirname" | wc -l '
    • @fahri 当你使用 set -x 时,bash 脚本的输出看起来就是这样,Jenkins 默认打开它。
    • 还有另一种方法可以在一行中执行命令吗?
    • @fahri 命令正在在一行中执行。如果您要问如何将 bash 的输出更改为仅在一行上,那是不可能的。如果它困扰您,您可以在 Groovy 代码中计算 find 产生的行数,然后您将能够删除 wc -l 部分。
    猜你喜欢
    • 1970-01-01
    • 2019-01-19
    • 2014-12-11
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多