2013-12-10 96 views
4

只是混淆了命令「ls *」的輸出。我在Ubuntu 11.10和Redhat Enterprise 6.3中都測試了腳本,得到了相同的結果。「ls *」的輸出不一致

殼牌登錄

$ uname -a 
    Linux 2.6.32-279.19.1.el6.x86_64 #1 SMP Sat Nov 24 14:35:28 EST 2012 x86_64 x86_64 x86_64 GNU/Linux 
$ echo $0 
    -bash 
$ cd test 
$ ls 
$ ls *    *<== at first, no file/directory under current work directory* 
ls: cannot access *: No such file or directory 
$ mkdir abc  *<== create a new directory "abc"* 
$ ls *    *<== output of "ls *" is empty but directory "abc" has been created.* 
$ mkdir abcd  *<== create the second directory "abcd"* 
$ ls *    *<== at this point, all the directories("abc" and "abcd") are displayed.* 
abc: 

abcd: 

任何人都可以解釋爲什麼後目錄 「ABC」 創建 「LS *」 的輸出是空的?謝謝。

回答

6

因爲ls *正在打印目錄abcd的內容,因此它是空的,因此您在輸出中看不到任何內容。 ls *打印當前目錄中所有目錄的所有文件和內容。

試試這個ls命令:

ls -d * 

,它會顯示在輸出abcd

man ls

-d  Directories are listed as plain files (not searched recursively). 
11

這主要是因爲glob模式是由外殼擴展,而不是由ls本身。

因此,創建一個空abc目錄,發行後:被調用

$ ls * 

結果ls,如果你鍵入:

$ ls abc 

,列出了abc內容。由於abc爲空,因此不會打印任何內容。

以同樣的方式,創造abcd,發行後:在ls

$ ls * 

結果被調用,如果你鍵入:

$ ls abc abcd 

而且,由於兩個目錄都通過了,ls將打印各在列出他們(空的)內容之前,將它們作爲標題。

6

這並不回答你的問題,但解決了*: no such file錯誤。

當未設置bash shell選項nullglob時,如果通配符擴展爲沒有文件,那麼通配符由shell作爲純文本字符串進行字面輸入。

$ ls 
$ ls * 
ls: cannot access *: No such file or directory 

有沒有文件,所以shell對*作爲一個普通的字符串。

接通nullglob選項和外殼將取代通配符什麼也沒有:

$ shopt -s nullglob 
$ ls * 
$ 
+0

證實你所說的,完全正確。謝謝。 – thinkhy