2013-03-20 58 views
1

我正在嘗試編寫一個簡單的函數,允許我交互地從Git中取消文件。這是我到目前爲止有:如何將每個文件名傳遞給我的函數?

reset_or_keep() { 
    echo "Received $1" 
} 

interactive_reset() { 
    local mods= 
    mods=`git status --porcelain | sed -e "s/^[A-Z][ \t]*//g" | xargs reset_or_keep` 
} 

不過,我收到此錯誤:

xargs: reset_or_keep: No such file or directory 

我想這reset_or_keep是在git status --porcelain

+2

我認爲這可以幫助你:http://stackoverflow.com/questions/11003418/calling-functions-with-xargs-within-a-bash-script – fedorqui 2013-03-20 22:18:14

回答

3

每個條目的最簡單方法是調用一次有效地用read直接在bash中實現xargs

#!/bin/sh 

reset_or_keep() { 
    echo "Received $1" 
} 

handle_files() { 
    while read filename ; do 
     reset_or_keep "$filename" 
    done 
} 

git status --porcelain | sed -e "s/^[ \t]*[A-Z][ \t]*//g" | handle_files 

(請注意,我不得不做出一個小的變化,以您的sed表達從我git status處理的輸出格式。)

注意的是,與xargs沒有-0標誌,該程序將無法正常使用文件工作包含空格的名稱。