2016-03-14 22 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