2016-03-14 63 views
1

這可能是一個愚蠢的錯誤。我使用this as a basis for a bash function嘗試爲this git function創建一個bash腳本:巴什腳本的Git分支列出

branch() { 
    if [[ [email protected] == "-r" ]]; then 
     command for k in `git branch -r | perl -pe 's/^..(.*?)(->.*)?$/\1/'`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1`\\t$k; done | sort -r 

else 
     command for k in `git branch | perl -pe s/^..//`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1`\\t$k; done | sort -r 

    fi 
} 

當我嘗試這個來源,它實際上是試圖執行的命令 - 然後給出因爲這個語法錯誤。

我能夠在本地執行命令,所以顯然我失去了一些東西在這裏創建我的功能顯而易見。

+0

在你的其他條款,即在GIT命令前單引號應該是一個反勾:'K在「git'成爲'K在\'git'。 – bishop

+0

@bishop改變這個結果的結果相同。 – enderland

+1

'command'具有'command [-pVv]命令[arg ...]'的用法。這不是'for'循環會給你帶來的。你想多次運行一個命令嗎?如果是這樣,把'command'放在'for'裏面。作爲一個建議,刪除「命令」,看看會發生什麼。 – bishop

回答

0

虛假command的一些簡化和去除後:

branch() { 
    local options="" 
    local pattern="s/^..//" 
    if [[ [email protected] == "-r" ]]; then 
     options="-r" 
     pattern="s/^..(.*?)(->.*)?$/\1/" 
    fi 
    git branch $options | perl -pe "$pattern" | while IFS= read -r k; do 
     branch_format $k 
    done | sort -r 
} 
branch_format() { 
    echo -e $(git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $1 -- | head -n 1) $1 
} 
+0

我建議'printf的 「%S \ t%S \ n」 「$(GIT秀...)」 「$ K」' –

+0

給了我足夠的基礎來弄清楚什麼我需要做的,謝謝(我編輯它包括我的工作功能)! – enderland

+0

不客氣。我做了類似的編輯,根據@glennjackman關於'for'迭代的註釋重構了一個更高性能的版本。獎金,使用'branch_format $ branch_name'來獲得一個格式化的版本,與'branch'中的使用無關。 – bishop