首页 > 解决方案 > 寻找匹配时意外的 EOF

问题描述

我有 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

标签: linuxshelljenkinsjenkins-groovy

解决方案


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

脚本:"""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')

推荐阅读