2017-04-11 41 views
0

我已經在Bamboo中編寫了一個部署計劃,它使用sh在代理上運行所有腳本,並且由於這些代理運行的是與破折號等同的ubuntu 14.04。如何在破折號中運行「rm -rf!(folder)」

我想要做以下步驟:

rm -rf !(${bamboo.scripts_folder}) 

這應該刪除所有文件/文件夾除了在變量的人。

這正常在bash,但是當我在衝刺跑這個它給了我:

dash: 2: Syntax error: "(" unexpected 

我試圖運行的腳本,以迫使它開始以下爲慶典而不是運行:

if [ "$(ps -p "$$" -o comm=)" != "bash" ]; then 
    bash "$0" "[email protected]" 
    exit "$?" 
fi 

儘管這看起來在過去已經爲我工作,在這種情況下,出於某種原因,它不會出現,因爲我得到:

line 8: syntax error near unexpected token `(' 

我試圖更新我的腳本來讀取:

#!/bin/bash 
echo "Early $0" 
if [ "$(ps -p "$$" -o comm=)" != "bash" ]; then 
    bash "$0" "[email protected]" 
    exit "$?" 
fi 

echo "Later $0" 

rm -rf !(${bamboo.scripts_folder}) 

我希望回聲報表將打印出目前正在運行的shell,而是我得到:

11-Apr-2017 13:51:25 Early /home/bamboo/bamboo-agent-home/temp/49020935-118947854-118064182-ScriptBuildTask-740980554529016045.sh 
11-Apr-2017 13:51:25 Early /home/bamboo/bamboo-agent-home/temp/49020935-118947854-118064182-ScriptBuildTask-740980554529016045.sh 
11-Apr-2017 13:51:25 Later /home/bamboo/bamboo-agent-home/temp/49020935-118947854-118064182-ScriptBuildTask-740980554529016045.sh 
11-Apr-2017 13:51:25 /home/bamboo/bamboo-agent-home/temp/49020935-118947854-118064182-ScriptBuildTask-740980554529016045.sh: line 13: syntax error near unexpected token `(' 
11-Apr-2017 13:51:25 /home/bamboo/bamboo-agent-home/temp/49020935-118947854-118064182-ScriptBuildTask-740980554529016045.sh: line 13: `rm -rf !(font)' 

這並沒有給我更多的東西繼續下去。任何人都可以建議「正確」的方式來運行這個使用破折號?或「正確」的方式,使該執行使用bash,而不是短跑(注意,竹啓動一切,SH,你不能改變)

+0

https://community.atlassian.com/t5/Bamboo-questions/Making-a-Bamboo -Script-execute-using-bin-bash/qaq-p/205364 –

+0

這就是我的問題,它不起作用 – Rumbles

+0

Extglobs是一個僅限bash的功能。作爲bash-only特性的一部分是,它......不存在*在不是bash(或類似擴展)的shell中。 –

回答

1

有沒有辦法?是。

你喜歡它嗎?可能不會。

set --; for d in */; do [ "$d" = "ignoreme/" ] && continue; set -- "[email protected]" "$d"; done 
rm -rf "[email protected]" 

這就是說,你的其他腳本沒有使用破折號在所有 - 至少,如果它被稱爲根據其家當。在這種情況下,它只是需要shopt -s extglob通過使extglobs,這是在默認情況下關閉擴展:

#!/bin/bash 

# you almost certainly don't need this at all 
[ -n "$BASH_VERSION" ] || [ -n "$ran_reexec" ] || ran_reexec=1 exec bash "$0" "[email protected]" 

shopt -s extglob  
rm -rf !(ignoreme) 
+0

它運行的破折號,因爲竹運行與sh的一切,並在ush sh是一個符號鏈接破折號... – Rumbles

+0

僅僅因爲它運行與sh的一切並不意味着sh運行*還*使用sh的一切。 'sh -c yourcommand'將表示'#!/ bin/bash',如果這是'yourcommand'開始的(並且它被標記爲可執行文件)。「sh yourcommand」當然不適用,但屬於「不這樣做」的範疇 - 使用'sh yourcommand'意味着如果它是一個編譯的二進制文件,Python的工具不能運行'yourcommand'腳本或其他*任何*但符合POSIX標準的腳本。 –

0

我知道我可以做:

find . ! -name 'folder' -delete -maxdepth 1 

有沒有找到的方法?