2012-09-01 38 views
0

我試着用函數來完成它:如何多次使用管道?

filter-root() { 
echo $1 | perl -pe 's/ /\n/g' | perl -pe 's/.*(emacs|libreoffice|autokey|bkubuntu).*//' | perl -pe 's/^\n//' 
} 

,但它不工作:

$ myList=`git ls-files` 
$ filter-root myList 
myList 

回答

2

你寫單獨bash變量時,你分配一個值給他們:

MYLIST=`ls *.txt` # Remember to avoid blanks aroud the "=" 

但如果你想bash擴大他們,你必須在他們前面加一個$

echo $MYLIST # Usually is "safer" to wrap the variable with quotes - echo "$MYLIST" 
1

您需要預先設置一個$傳遞變量:

filter-root $myList 

而且你應該通過"$myList"來防止myList的內容被Bash拆分成令牌或者你可以使用echo "$*"

+0

確實如此。其他方法也是我做錯的方式 - 它給出了「手動管道」的不同結果。 – Adobe

+1

哦 - 我解決了它:'filter-root「$ myList」'。感謝Avio--實際上我有一個字符串 - 所以我不認爲需要quoutes。但它只適用於這種方式。 – Adobe