2011-09-08 56 views
0

錯誤我發現下面我們老的腳本功能:問題有關KSH

f() { # < files list 
    typeset file 
    cat - > $TMPFILE # Bug in KSH 
    while read -r file 
    do 
     process $file 
    done < $TMPFILE 
} 

有誰知道在KSH這個bug?

+0

在http://unix.stackexchange.com/上可能會做得更好 – Julian

回答

2

顯然,f()是一個過濾功能,即你應該使用它在管道像這樣

./generate_filelist.sh | f 

在你期望read -r讀標準輸入就好了,例如做

./generate_filelist.sh | while read -r file; do echo $file; done 

時顯然,有(是?)在(某些)KSH,阻止同樣的從功能工作(版本(S))的錯誤:

f() { 
    typeset file 
    while read -r file # whoops not reading from stdin as it should? 
    do 
     process $file 
    done 
} 

我foun 2臭蟲可能是它,這取決於主要是你的平臺上:

有可能(很多)更多的歷史錯誤可以應用,我停止搜索,因爲我不知道任何關於您的平臺,或實際上腳本的設計平臺。

所以相反,有一種解決方法,將stdin寫入臨時文件並從中讀取。需要注意的是

  • 這改變語義(後只收到完整的文件閱讀開始)
  • 好像那裏是你的樣品中一個錯字($$TMPFILE,可能代替$TMPFILE?)(除非有KSH的另一種特性我不知道的,$$擴展到當前進程ID))

f() { 
    typeset file 
    cat - > $TMPFILE 
    while read -r file 
    do 
     process $file 
    done < $TMPFILE 
} 
+0

平臺 - AIX。 –