2016-10-22 94 views
0

我想寫一個腳本來使用「SSH EXEC」類型構建步驟在teamcity中重新啓動我的服務器。嵌套的bash命令不工作在teamcity ssh exec

腳本必須做的事情之一是解壓縮上一步中我上傳的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] 
+3

爲什麼不只是'unzip * .zip'? – Barmar

+0

我也使用這種嵌套的命令風格來執行其他命令。所以我仍然想知道爲什麼嵌套命令不起作用。謝謝你的信息!我不知道解壓縮可能需要一個正則表達式 –

+0

通配符由shell擴展,它們自動適用於每個命令。 – Barmar

回答

3

這就是當沒有參數時解壓輸出。我的猜測是ls | grep zip返回一個空字符串,因爲ls沒有找到任何東西。

我認爲這是本地shell如何處理輸入到ssh的字符串的工件。當你從一個地方的bash shell做

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

,bash中看到,有一個嵌入式子shell和第一本地執行ls | grep zip,並將結果嵌入到字符串,並將結果字符串爲SSH。由於(大概)有在執行此SSH命令將當前目錄中沒有zip文件,傳遞到遠程shell的實際命令變得

ssh $host "unzip " 

解決這個問題的辦法是逃避反引號,使他們不由bash解釋,但嵌入並轉發到遠程外殼:

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

或'ssh $ host'解壓$(ls | grep zip)''。單引號可防止bash解釋任何內容 –