【问题标题】:Nested bash command not working in teamcity ssh exec嵌套的 bash 命令在 teamcity ssh exec 中不起作用
【发布时间】:2017-03-04 08:58:34
【问题描述】:

我正在尝试使用“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]

【问题讨论】:

  • 为什么不直接unzip *.zip
  • 我也在使用这种嵌套命令样式来执行其他命令。所以我仍然想知道为什么嵌套命令不起作用。谢谢你的信息!我不知道 unzip 可以使用正则表达式
  • 通配符由shell扩展,它们自动适用于每个命令。
  • 这不是正则表达式,而是通配符 AKA glob。

标签: bash ssh teamcity


【解决方案1】:

这就是没有参数时 unzip 输出的内容。我的猜测是 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\`"

【讨论】:

  • ssh $host 'unzip $(ls | grep zip)'。单引号可防止 bash 解释任何内容
猜你喜欢
  • 2023-04-05
  • 2014-11-19
  • 1970-01-01
  • 2014-04-05
  • 2016-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
相关资源
最近更新 更多