怎么理解ls `ls`这个命令?

sh/bash/dash/ksh/zsh等Shell脚本
回复
Dic4000
帖子: 104
注册时间: 2008-09-10 21:06

怎么理解ls `ls`这个命令?

#1

帖子 Dic4000 » 2008-11-20 22:49

在终端下执行 ls `ls`(那个 ` 是键盘上方的数字鍵 1 左边那个按键,而不是单引号!)
输出结果第一行报错,但从第二行开始显示了当前目录下的所有文件(子文件夹中的文件也被递归的显示出来了)
不明白为什么会产生这个结果? :em20
头像
kofshower
帖子: 1343
注册时间: 2007-03-13 11:23
联系:

Re: 怎么理解ls `ls`这个命令?

#2

帖子 kofshower » 2008-11-20 22:56

理解上存在讹误,没有递归,才两层。ls -R 才是递归.此外,你的ls是哪个ls,定义alias了把:
"We are all in the mud, but some of us are looking at the stars." (Oscar Wilde)
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
Dic4000
帖子: 104
注册时间: 2008-09-10 21:06

Re: 怎么理解ls `ls`这个命令?

#3

帖子 Dic4000 » 2008-11-20 22:59

第一行报的错是: ls: 无法访问主题: 没有该文件或目录
但在我的当前目录下有这个名为“主题"的文件夹,用ls -l命令 能查询出来,它的权限为rwxr-xr-x,但用命令ls 主题 却报错,错误信息也是 ls: 无法访问主题: 没有该文件或目录
Dic4000
帖子: 104
注册时间: 2008-09-10 21:06

Re: 怎么理解ls `ls`这个命令?

#4

帖子 Dic4000 » 2008-11-20 23:01

ls,没有定义alias,就是系统的 内建的命令。
头像
kofshower
帖子: 1343
注册时间: 2007-03-13 11:23
联系:

Re: 怎么理解ls `ls`这个命令?

#5

帖子 kofshower » 2008-11-20 23:15

看看自己的env
"We are all in the mud, but some of us are looking at the stars." (Oscar Wilde)
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

Re: 怎么理解ls `ls`这个命令?

#6

帖子 xhy » 2008-11-20 23:18

有文件名中间带IFS了 通常是个空格
目前负债150多万
Dic4000
帖子: 104
注册时间: 2008-09-10 21:06

Re: 怎么理解ls `ls`这个命令?

#7

帖子 Dic4000 » 2008-11-20 23:59

xhy 写了:有文件名中间带IFS了 通常是个空格
恩,谢谢了,文件命后面跟了个空格,所以报了错。文件名不能有空格吗?

能否再帮我解释下以下两个命令为什么会显示不同的结果:
ls|ls
ls `ls`
管道与引用有点混淆不清了:它们都是把一个命令的输出作为另一个命令的输入吗? :em06
头像
kofshower
帖子: 1343
注册时间: 2007-03-13 11:23
联系:

Re: 怎么理解ls `ls`这个命令?

#8

帖子 kofshower » 2008-11-21 0:40

\来转义
"We are all in the mud, but some of us are looking at the stars." (Oscar Wilde)
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

Re: 怎么理解ls `ls`这个命令?

#9

帖子 xhy » 2008-11-21 0:51

a | b a命令的结果通过管道传递给b,并不是作为参数,效果相当于b运行后从终端取得输入

b `a` a命令的结果直接作为调用b命令的参数

一个命令不一定同时支持管道和参数,或者对于两个地方的输入行为定义不一样
目前负债150多万
Dic4000
帖子: 104
注册时间: 2008-09-10 21:06

Re: 怎么理解ls `ls`这个命令?

#10

帖子 Dic4000 » 2008-11-21 21:46

太感谢你了:
xhy 写了:a | b a命令的结果通过管道传递给b,并不是作为参数,效果相当于b运行后从终端取得输入
刚才测试发现如果从终端取得的输入对于a命令毫无意义的话,整个命令a | b还是可以运行成功,不过效果就相当于只执行b命令了。如exit | ls
goomoott2
帖子: 1
注册时间: 2007-03-14 21:41

Re: 怎么理解ls `ls`这个命令?

#11

帖子 goomoott2 » 2008-11-22 15:08

想将ls的返回结果给ls做参数的话,可以这样做

代码: 全选

ls | xargs ls $*
Dic4000
帖子: 104
注册时间: 2008-09-10 21:06

Re: 怎么理解ls `ls`这个命令?

#12

帖子 Dic4000 » 2008-11-23 0:19

goomoott2 写了:想将ls的返回结果给ls做参数的话,可以这样做

代码: 全选

ls | xargs ls $*
请问这个$*怎么解释?
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

Re: 怎么理解ls `ls`这个命令?

#13

帖子 xhy » 2008-11-23 0:29

Dic4000 写了:
goomoott2 写了:想将ls的返回结果给ls做参数的话,可以这样做

代码: 全选

ls | xargs ls $*
请问这个$*怎么解释?

代码: 全选

Expands to the positional parameters, starting from one.  When the  expansion  occurs  within  double  quotes,  it
expands  to  a  single  word  with the value of each parameter separated by the first character of the IFS special
variable.  That is, "$*" is equivalent to "$1c$2c...", where c is the first character of  the  value  of  the  IFS
variable.   If  IFS  is  unset, the parameters are separated by spaces.  If IFS is null, the parameters are joined
without intervening separators.
实际上不需要$*,

代码: 全选

ls | xargs ls
就够了
目前负债150多万
回复