只是混淆了命令“ls *”的输出。 我在Ubuntu 11.10和Redhat Enterprise 6.3下都testing了脚本,得到了相同的结果。
Shell日志
$ 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”创build后,“ls *”的输出为空? 谢谢。
这主要是因为glob模式被shell扩展,而不是由ls
本身扩展。
因此,在创建一个空的abc
目录之后,发出:
$ ls *
ls
被调用的结果就像你输入的那样:
$ ls abc
其中列出了abc
的内容。 由于abc
是空的,所以没有打印。
同样,在创建abcd
,发出:
$ ls *
ls
被调用的结果就像你输入的那样:
$ ls abc abcd
而且由于两个目录都被传递,所以ls
将在列出它们(空的)内容之前将它们打印成一个头。
因为ls *
正在打印abcd
目录中的内容,因此您在输出中看不到任何内容。 ls *
打印当前目录中所有目录的所有文件和内容 。
试试这个ls
命令:
ls -d *
这将在输出中显示abcd
。
从man ls
-d Directories are listed as plain files (not searched recursively).
这不回答你的问题,但解决*: no such file
错误。
当bash shell选项nullglob
设置时,如果通配符扩展为无文件 ,那么通配符由shell作为一个纯字符串进行字面输入。
$ ls $ ls * ls: cannot access *: No such file or directory
没有文件,所以shell将*视为纯字符串。
打开nullglob
选项,shell将用nullglob
代替通配符:
$ shopt -s nullglob $ ls * $