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

时间:2016-10-22 00:03:27

标签: bash ssh teamcity

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

脚本必须做的一件事是解压缩我在上一步中上传的zip文件,但文件名中的版本将始终更改,所以我试图使用嵌套命令查找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]

1 个答案:

答案 0 :(得分:3)

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

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

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

从本地bash shell中,bash看到有一个嵌入式子shell并首先在本地执行ls | grep zip,并将结果嵌入到字符串中,并将结果字符串传递给ssh。由于(推测)当前目录中没有执行此ssh命令的zip文件,因此传递给远程shell的实际命令变为

ssh $host "unzip "

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

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