shell命令xargs

shell命令xargs

build and execute command lines from standard input

xargs 可以读入 stdin 的数据,并且以空格符或断行字符作为分辨,将 stdin 的数据分隔成为 arguments

1)说明

xargs [-0epn] command

选项与参数:

-0 如果输入的 stdin 含有特殊字符,例如 `, \, 空白键等等字符时,这个 -0 参数可以将他还原成一般字符。

-e 这个是 EOF (end of file) 的意思。后面可以接一个字符串,当 xargs 分析到这个字符串时,就会停止继续工作!

-p 在执行每个指令的 argument 时,都会询问使用者的意思;

-n 后面接次数,每次 command 指令执行时,要使用几个参数的意思。

xargs 后面没有接任何的指令时,预设是以 echo 来进行输出!

2)示例

cut -d ":" -f1 /etc/passwd | tail -n 5 | xargs finger

3)使用 xargs 的原因是, 很多指令其实并不支持管道命令,因此我们可以透过 xargs 来提供该指令引用 standard input 之用。

find ./ -name prime.cpp | ls -l //输出并不是我们所想,因为ls不支持管道

find ./ -name prime.cpp | xargs ls -l

原文

[1]http://linux.vbird.org/linux_basic/0320bash.php

posted @ 2013-04-21 18:11  浪里飞  阅读(813)  评论(0编辑  收藏  举报