2014-04-04 20 views
0

我想在當前目錄中處理一組文件(* .ui)。如果找到一些* .ui文件,以下腳本將按預期工作。但是,如果當前目錄中不存在.ui文件,則for循環將全部輸入相同。這是爲什麼 ?bash:for不存在的文件的循環

for f in *.ui 
do 
    echo "Processing $f..." 
done 

它打印:

Processing *.ui... 
+0

FAQ:'shop -s nullglob' – devnull

回答

1

用途:

shopt -s nullglob 

man bash

了nullglob 如果設置,bash允許拍不匹配文件的terns(參見上面的路徑名擴展 )擴展爲空字符串,而不是它們自己。

+0

我喜歡這個解決方案,因爲它接近於C/C++。更多細節在這裏:http://mywiki.wooledge.org/NullGlob – dplamp

+0

好的!這個標誌的默認值是什麼?或者有什麼方法來解除它? – fedorqui

+0

使用'shopt -u'來禁用和'shopt -s'來啓用。 – dogbane

0

你已經有了如何,「爲什麼」是bash將首先嚐試匹配*.ui的文件,但如果不工作(它會沒有結果),它會假設你的意思是字符串"*.ui"

for f in "*.ui" 
do 
    echo "Processing $f..." 
done 

WIL確實打印"Processing *.ui"