嵌套 bash 命令在 teamcity ssh exec 中不起作用

Nested bash command not working in teamcity ssh exec

我正在尝试编写脚本以使用 "SSH EXEC" 类型构建步骤在 teamcity 中重启我的服务器。

脚本必须做的一件事是解压缩我在上一步中上传的 zip 文件,但是文件名中的版本总是会改变,所以我正在尝试使用嵌套命令。

unzip `ls | grep zip`

当我通过 ssh 连接到我的服务器并在终端中执行此操作时,这对我有用,但当 teamcity 尝试执行此操作时,它不起作用。我在构建日志中收到以下消息

[19:36:55][Step 3/3] UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.
[19:36:55][Step 3/3] 
[19:36:55][Step 3/3] Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
[19:36:55][Step 3/3]   Default action is to extract files in list, except those in xlist, to exdir;
[19:36:55][Step 3/3]   file[.zip] may be a wildcard.  -Z => ZipInfo mode ("unzip -Z" for usage).
[19:36:55][Step 3/3] 
[19:36:55][Step 3/3]   -p  extract files to pipe, no messages     -l  list files (short format)
[19:36:55][Step 3/3]   -f  freshen existing files, create none    -t  test compressed archive data
[19:36:55][Step 3/3]   -u  update files, create if necessary      -z  display archive comment only
[19:36:55][Step 3/3]   -v  list verbosely/show version info       -T  timestamp archive to latest
[19:36:55][Step 3/3]   -x  exclude files that follow (in xlist)   -d  extract files into exdir
[19:36:55][Step 3/3] modifiers:
[19:36:55][Step 3/3]   -n  never overwrite existing files         -q  quiet mode (-qq => quieter)
[19:36:55][Step 3/3]   -o  overwrite files WITHOUT prompting      -a  auto-convert any text files
[19:36:55][Step 3/3]   -j  junk paths (do not make directories)   -aa treat ALL files as text
[19:36:55][Step 3/3]   -U  use escapes for all non-ASCII Unicode  -UU ignore any Unicode fields
[19:36:55][Step 3/3]   -C  match filenames case-insensitively     -L  make (some) names lowercase
[19:36:55][Step 3/3]   -X  restore UID/GID info                   -V  retain VMS version numbers
[19:36:55][Step 3/3]   -K  keep setuid/setgid/tacky permissions   -M  pipe through "more" pager
[19:36:55][Step 3/3]   -O CHARSET  specify a character encoding for DOS, Windows and OS/2 archives
[19:36:55][Step 3/3]   -I CHARSET  specify a character encoding for UNIX and other archives
[19:36:55][Step 3/3] 
[19:36:55][Step 3/3] See "unzip -hh" or unzip.txt for more help.  Examples:
[19:36:55][Step 3/3]   unzip data1 -x joe   => extract all files except joe from zipfile data1.zip
[19:36:55][Step 3/3]   unzip -p foo | more  => send contents of foo.zip via pipe into program more
[19:36:55][Step 3/3]   unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer
[19:36:56][Step 3/3] SSH exit-code [0]

当没有参数时,这就是 unzip 输出的内容。我的猜测是 ls | grep zip 返回一个空字符串,因为 ls 没有找到任何东西。

我认为这是本地 shell 如何处理输入到 ssh 的字符串的产物。当你这样做时

ssh $host "unzip `ls | grep zip`"

from a local bash shell, bash 看到有嵌入的subshell 就先在本地执行ls | grep zip, 嵌入结果进入字符串,并将生成的字符串传递给 ssh。由于(大概)在执行此 ssh 命令的当前目录中没有 zip 文件,因此传递给远程的实际命令 shell 变为

ssh $host "unzip "

解决这个问题的方法是转义反引号,这样它们就不会被 bash 解释,而是嵌入并转发到远程 shell:

ssh $host "unzip \`ls | grep zip\`"