求教,管道与xargs。

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
回复
头像
tiyuanmaster
帖子: 73
注册时间: 2009-06-14 20:14
联系:

求教,管道与xargs。

#1

帖子 tiyuanmaster » 2009-06-29 21:12

之前发了个贴,问题可能没有问清楚,现在再发一个,请各位不要嘲笑,本人初学者,请不吝赐教。

请教一下:比如说 echo aaa | cat 那么输出就会是aaa。
echo aaa | xargs cat 就会输出aaa文件的内容。
那么就是说 echo aaa | cat 并不等同于cat aaa,而是echo aaa | xargs cat 等同于cat aaa。那这样管道到底什么作用?为什么管道不能直接将aaa参数作为一个文件名传递给cat命令。而是非要加上xargs,那么xargs又是什么作用呢?
其实我是不懂装懂的…………………………
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 求教,管道与xargs。

#2

帖子 HuntXu » 2009-06-29 21:20

man 7 pipe:
Pipes and FIFOs (also known as named pipes) provide a unidirectional interprocess communication channel. A pipe has a read end and a write end. Data written to the write end of a pipe can be read from the read end of the pipe.
man xargs:
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.
怎么会搞混的呢,都是不同的东西啊
HUNT Unfortunately No Talent...
头像
tiyuanmaster
帖子: 73
注册时间: 2009-06-14 20:14
联系:

Re: 求教,管道与xargs。

#3

帖子 tiyuanmaster » 2009-06-30 11:26

终于搞明白了,是把参数和标准输入搞混淆了,有些命令可以由参数提供运行所需的必要信息,也可以由标准输入提供,比如cat,grep,sort,等。但是有些比如ls,rm,du等就只能由参数提供,如果想借助于标准输入就用xargs命令将标准输入的内容作为命令的参数。
其实我是不懂装懂的…………………………
回复