Add New Notes

This commit is contained in:
geekard
2012-08-08 14:26:04 +08:00
commit 5ef7c20052
2374 changed files with 276187 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2011-11-23T16:32:12+08:00
====== XARGS-openbsd ======
Created Wednesday 23 November 2011
http://www.rocketaware.com/man/man1/xargs.1.htm
XARGS(1) OpenBSD Reference Manual XARGS(1)
NAME
xargs - construct argument list(s) and execute utility
SYNOPSIS
xargs [-0] [-t] [[-x]-n number] [-s size] [utility [arguments ...]]
DESCRIPTION
The xargs utility reads space, tab, newline, and end-of-file delimited
arguments from the standard input and executes the specified utility with
them as arguments.
The utility and any arguments specified on the command line are given to
the utility upon each invocation, followed by some number of the argu-
ments read from standard input. The utility is repeatedly executed until
standard input is exhausted.
Spaces, tabs and newlines may be embedded in arguments using single (`'')
or double (`"') quotes or backslashes (`\'). Single quotes escape all
non-single quote characters, excluding newlines, up to the matching sin-
gle quote. Double quotes escape all non-double quote characters, exclud-
ing newlines, up to the matching double quote. Any single character, in-
cluding newlines, may be escaped by a backslash.
The options are as follows:
-0 Changes xargs to expect NUL (`\0') characters as separators, in-
stead of spaces and newlines. This is expected to be used in
concert with the -print0 function in find(1).
-n number
Set the maximum number of arguments taken from standard input for
each invocation of the utility. An invocation of utility will
use less than number standard input arguments if the number of
bytes accumulated (see the -s option) exceeds the specified size
or there are fewer than number arguments remaining for the last
invocation of utility. The current default value for number is
5000.
-s size
Set the maximum number of bytes for the command-line length pro-
vided to utility. The sum of the length of the utility name and
the arguments passed to utility (including null terminators) will
be less than or equal to this number. The current default value
for size is ARG_MAX - 2048.
-t Echo the command to be executed to standard error immediately be-
fore it is executed.
-x Force xargs to terminate immediately if a command line containing
number arguments will not fit in the specified (or default) com-
mand-line length.
If no utility is specified, echo(1) is used.
Undefined behavior may occur if utility reads from the standard input.
The xargs utility exits immediately (without processing any further in-
put) if a command line cannot be assembled, utility cannot be invoked, an
invocation of the utility is terminated by a signal, or an invocation of
the utility exits with a value of 255.
DIAGNOSTICS
xargs exits with one of the following values:
0 All invocations of utility returned a zero exit status.
123 One or more invocations of utility returned a nonzero exit sta-
tus.
124 The utility exited with a 255 exit status.
125 The utility was killed or stopped by a signal.
126 The utility was found but could not be invoked.
127 The utility could not be found.
1 Some other error occurred.
SEE ALSO
echo(1), find(1)
STANDARDS
The xargs utility is expected to be IEEE Std1003.2 (``POSIX.2'') compli-
ant.
HISTORY
The meaning of 123, 124, and 125 exit values were taken from GNU xargs.
OpenBSD 2.6 June 6, 1993 2

59
Zim/Utils/xargs/xarg.txt Normal file
View File

@@ -0,0 +1,59 @@
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2011-04-21T17:05:01+08:00
====== xarg ======
Created Thursday 21 April 2011
Man page of 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.
我的理解xargs从标准输入读取由空格或者换行符分隔的条目并将其作为xargs后面所跟命令参数的一部分默认使用/bin/echo所接的命令可以带参数然后执行命令。
常用参数:
-f file 从文件中读取条目而不是从标准输入;
-0 读取的条目以null作为结束而不是以空格或换行符进行分隔
-d delim 指定分隔符;
--verbose 执行命令前显示所执行的命令;
-n max-args 指定参数个数
例1xargs verbose #从标准输入读,使用-f可从文件读参数
输入hello world (Ctrl + D)
输出:/bin/echo hello world #--verbose显示的执行命令
hello world
例2ls /home/ydzhang/xargs | xargs rm rf #从管道读
输入:/home/ydzhang/xargs目录下的条目
输出rm rf a b c a,b,c为/home/ydzhang/xargs下的文件
例3find /home/ydzhang | xargs grep “hello” #从管道读
输入:/home/ydzhang下的所有文件名
输出:/home/ydzhang目录下所有包含hello的文件对应的行
例4find /home/ydzhang print0 | xargs -0 grep “hello” #从管道读
输入以null作为字符串结束符的文件名序列-print0将这些参数传给xargs时如果使用默认参数xargs将不能正确解析必须告诉xargs这个信息即使用-0参数。
输出:/home/ydzhang目录下所有包含hello的文件对应的行
注意find 的-print0效果类似-print但是将每行输出信息末端的\n字符换为\0字符如果find找出的文件名列表中含有空格这时就必须用-print0代替-print .

View File

@@ -0,0 +1,78 @@
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2011-11-23T15:41:16+08:00
====== xargs 命令 ======
Created Wednesday 23 November 2011
http://www.chinaunix.net/jh/24/409543.html
=== 用途 ===
构造参数列表并运行命令。
=== 语法 ===
xargs [ -p ] [ -t ] [ -e__[__ EOFString __]__ ] [ -E EOFString ] [ -i__[__ ReplaceString __]__ ] [ -I ReplaceString ] [ -l[ Number ] ] [ -L Number ] [ -nNumber [ -x ] ] [ -sSize ] [ Command [ Initial-Argument ... ] ]
注: __不要__在小写的标志和参数之间放置空格。
==== 描述 ====
生成的命令行长度size, 由 -s 控制)是 Command 和每个作为字符串对待的 Argument(包括用于每个字符串的空字节结束符号)大小(以字节计算)的总和。
xargs 命令可以限制命令行的长度。当__构造__的命令行运行时组合的 Argument 和环境列表不能超过 ARG_MAX 字节。在这一约束里,如果不指定 -n 或 -s 标志,缺省命令行长度至少是 LINE_MAX 指定的值。
=== 标志 ===
-e[EOFString]废弃的标志。请使用 -E 标志。
将 EOFString 参数用作逻辑 EOF 字符串。如果不指定 -e 或 -E 标志则采用下划线_为逻辑 EOF 字符串。如果不指定
EOFString 参数,逻辑 EOF 字符串能力被禁用且下划线被照字面含义使用。xargs 命令读取标准输入__直到__达到 EOF 或指定的字符串。
-E EOFString指定逻辑 EOF 字符串以替换缺省的下划线_。 xargs 命令读取标准输入直到达到 EOF 或指定的字符串。
-i[ReplaceString]废弃的标志。请使用 -I大写 i标志。
如果没有指定 ReplaceString 参数,使用字符串 "__{}__"。
注:-I大写 i和 -i 标志是互相排斥的;最后指定的标志生效。
-I ReplaceString大写 i
用从标准输入读取的字符串替换__Initial-Arguments__中的每一个ReplaceString, ReplaceString是指定的__标记字符串.__ReplaceStrings 不能在超过 5 个自变量中使用。在每个标准输入行开始的空字符被忽略。每个 Argument 能包含一个或多个 ReplaceStrings但不能大于 255 字节。-I 标志同样打开 -x 标志。
注:-I大写 i和 -i 标志是互相排斥的;最后指定的标志生效。
-l[Number](小写的 L。废弃的标志。请使用 -L 标志。
如果没有指定 Number 参数,使用缺省值 1。-l 标志同样打开 -x 标志。
注: __-L、-l小写的 L和 -n 标志是互相排斥的__最后指定的标志生效。
-L Number 指定从标准输入读取的非空行的数目,
如果保留少于指定的 NumberCommand 参数的最后调用可以有少数几个参数行。一行以第一个换行字符结束,除非行的最后一个字符是一个空格或制表符。后续的空格表示 延续至下一个非空行。
-n Number 指定一次添加到command后的从标准输入读取的参数的个数.
如果被积累的命令行长度超过了由 -s Size 标志指定的字节, 则最后实际添加的参数少于 Number但是非零
注: -L、-I小写的 L和 -n 标志是互相排斥的;最后指定的标志生效。
-p __ 询问是否运行__构造好 参数列表的Command 。它显示构造的命令行,后跟一个 ?...(问号和省略号)提示。输入肯定的、特定于语言环境的响应以运行 Command 参数。任何其它响应都会引起 xargs 命令跳过那个特定的参数调用。每个调用都将询问您。 -p 标志同样打开 -t 标志。
-s Size 设置构造的 Command 行的__最大字符数__。Size 参数必须是正整数。如果满足以下条件,则使用很少的自变量:
自变量的总数超出 -n 标志指定的自变量数。 总行数超出 -L 或 -I小写 L标志指定的行数。 累加至在 Size 参数指定的字节数之前达到 EOF。
-t启用__跟踪方式__并在运行之前将构造的 Command 行回送到标准错误。
-x如果有任何 Command 行大于 -s Size 标志指定的字节数__停止运行__ xargs 命令。如果指定 -I大写 i或 -l小写
L标志则打开 -x 标志。如果没有指定 -i、-I大写 i、-l小写 L、-L 或 -n 标志,则 Command 行的总长度必须在
-s Size 标志指定的限制内。
=== 出口状态 ===
该命令返回下列出口值:
0 if it succeeds
123 if any invocation of the command exited with status 1-125
124 if the command exited with status 255
125 if the command is killed by a signal
126 if the command cannot be run
127 if the command is not found
1 if some other error occurred.

View File

@@ -0,0 +1,71 @@
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2011-11-23T15:24:57+08:00
====== xargs命令少为人知的细节 ======
Created Wednesday 23 November 2011
http://www.yayu.org/look.php?id=179
与xargs命令最初相识是在发现一个磁盘满了具体是在/var/spool/clientmqueue主要原因是系统中有用户开启了crontab而crontab中执行的程序有输出内容输出内容会以邮件形式发给cron的用户而sendmail没有启动所以就产生了这些文件。关于更详细的原理可以参考我之前的文章crontab命令的使用介绍及我的体会。
仅仅解决出现多文件的方法比较简单,在命令后加上“> /dev/null 2>&1”即可表示程序员输出和运行错误都放到黑洞里面去这样就不会产生文件了。
如果是要解决删除多文件的问题则进入这个文件夹执行“ls | xargs rm -f ”即可。xargs可以从管道中循环读取文件一次一次的把信息输送给后面的“rm -f”。
请注意以上措辞“一次一次的”那么这个一次一次是指“一个一个”还是“__一批一批__”呢
很杯具当时不求甚解未能深入学习xargs误解为“一个一个”。
最近在写一个程序时需要处理一个文件中的行数据。平时都是使用php的fopen再fgets解决问题但这次懒得套用这一套了于是想使用管道把数据传送给php脚本。
而php脚本也利用$argv这个数组来获取命令行输入的参数那么很简单的获取$argv[1]就可以了。如“php a.php b”$argv[0]为文件名a.php$argv[1]就是后面的参数b了。于是想当然的cat uid.txt | xargs php a.php 。
最终发现uid.txt中有近4千行的数据但是只处理了4行。那么既然出现问题就边解决边学习吧
使用cat uid.txt | xargs echo > file.out
发现 file.out文件果然就是四行但是每行都很长。。。。。
如下uid.txt内容为“......”表示很多行)
以下是引用片段:
1234567890
......
2234567890
......
3234567890
......
4234567890
......
那么file.out的结果为
以下是引用片段:
第一行1234567890 ......
第二行2234567890 ......
第三行3234567890 ......
第四行4234567890 ......
所以程序处理每一行的第一个了剩下的全部被忽略。而我期望的结果是xargs每次给我一行。
那么寻求man的帮助吧
以下是引用片段:
--max-chars=max-chars, __-s max-chars __#限定生成的命令行参数包含的字符数目
Use at most max-chars characters **per command line**, including the command and initial-arguments and the
terminating nulls at the ends of the argument strings. The default is __131072 __characters, not including
the size of the environment variables (which are provided for separately so that it doesnt matter if
your environment variables take up more than 131072 bytes). The operating system places limits on the
values that you can usefully specify, and if you exceed these a warning message is printed and the
value actually used is set to the appropriate upper or lower limit.
--max-args=max-args,__ -n max-args __#限定每次附加到命令行后的从标准输入读取的参数的个数(不包括已有的参数)
Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the
size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.
更进一步的:
1其实每个系统对于参数列表的大小都有限制。比如__ARG_MAX__一般至少定义为4096 bytes。如果超过了ARG_MAX将产生shell错误Argument list too lang这个问题可以用上面说的xargs命令解决问题。
2xargs的-s参数根据实战的结果貌似会在指定的值和真实数据中取得平衡不会只依据-s指定的大小活生生的把源数据的一行撕裂成两行。

View File

@@ -0,0 +1,70 @@
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2011-11-23T15:23:04+08:00
====== xargs详解 ======
Created Wednesday 23 November 2011
http://www.linuxsense.org/archives/118.html
Linux Shell真的是一个比较好玩的东西以前也会经常写一些不过都是些简单的东西或者称之为批处理估计也只能叫批处理只是一些简单的命令的堆积。:)。
今天稍微看了一下xargs一个简单的示例应用如下
[root@TestServer haha]#ls
a.class b.class c.class
a.java b.java c.java
[root@TestServer haha]#ls |xargs rm
[root@TestServer haha]#ls
[root@TestServer haha]#
呵呵虽然有点避简就烦但是能说明了xargs的功能它将输入输出给xargs后面的命令作为那个命令的参数。
也就是说,上面的命令执行相当于:
rm a.class b.class c.class a.java b.java c.java
xargs将前面的ls的结果交给了xargs后面的rm命令作为rm命令的参数。它真正的含义可以用xargs自己来解释
[root@TestServer haha]# cat a
first line
second line
[root@TestServer haha]# cat a |xargs
**first line second line**
[root@TestServer haha]# cat a |xargs __verbose__
**/bin/echo first line second line**
first line second line third line
[root@TestServer haha]# cat a |xargs verbose__ max-args=1__
/bin/echo first
first
/bin/echo line
line
/bin/echo second
second
/bin/echo line
line
——————————————————————————–
还有人这样解释xargs
xargs的作用是用来回避对命令行长度的限制它通过使用一个参数多次调用一个命令实现这一功能而不是一次使用多个参数
。这句话的意思是xargs永远一次只传给他后面的命令一个参数然后不停的将所有参数一一传完然而这个说法是错误的下面的试验将证明这个说法是不正确的
[root@TestServer haha]# ll
total 12
-rw-rr 1 root root 34 Aug 19 20:43 a
-rw-rr 1 root root 34 Aug 19 20:45 b
drwxr-xr-x 2 root root 4096 Aug 19 20:54 c_folder
[root@TestServer haha]# ll c_folder/
total 0
[root@TestServer haha]# **ls| xargs cp**
[root@TestServer haha]# ll
total 12
-rw-rr 1 root root 34 Aug 19 20:43 a
-rw-rr 1 root root 34 Aug 19 20:45 b
drwxr-xr-x 2 root root 4096 Aug 19 20:54 c_folder
[root@TestServer haha]# ll c_folder/
total 8
-rw-rr 1 root root 34 Aug 19 20:54 a
-rw-rr 1 root root 34 Aug 19 20:54 b
[root@TestServer haha]#
从这个试验可以看到xargs确实是将a b c_folder三个参数__同时__传给了cp命令不然不可能出现这样的结果。

View File

@@ -0,0 +1,98 @@
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2011-11-23T15:29:25+08:00
====== 记录一linux命令xargs ======
Created Wednesday 23 November 2011
http://blog.csdn.net/chinalinuxzend/article/details/3327602
大多数 Linux 命令都会产生输出:**文件列表、字符串列表**等。但如果要使用其他某个命令并将前一个命令的输出作为参数该怎么办例如file 命令显示文件类型可执行文件、ascii 文本等);你能处理输出,使其仅显示文件名,目前你希望将这些名称传递给 ls -l 命令以查看时间戳记。xargs 命令就是用来完成此项工作的。他允许你对输出执行其他某些命令。记住下面这个来自于第 1 部分中的语法:
//file -Lz * | grep ASCII | cut -d":" -f1 | xargs ls -ltr//
让我们来剖析这个命令字符串。第一个file -Lz *,用于查找是符号链接或经过压缩的文件。他将输出传递给下一个命令 grep ASCII该命令在其中搜索 "ASCII" 字符串并产生如下所示的输出: alert_DBA102.log: ASCII English text
alert_DBA102.log.Z: ASCII text (compressd data 16 bits)
dba102_asmb_12307.trc.Z: ASCII English text (compressd data 16 bits)
dba102_asmb_20653.trc.Z: ASCII English text (compressd data 16 bits)
由于我们只对文件名感兴趣,因此我们应用下一个命令 cut -d":" -f1仅显示第一个字段 alert_DBA102.log
alert_DBA102.log.Z
dba102_asmb_12307.trc.Z
dba102_asmb_20653.trc.Z
目前,我们希望使用 ls -l 命令将上述列表作为参数进行传递一次传递一个。xargs 命令允许你这样做。最后一部分xargs ls -ltr用于接收输出并对其执行 ls -ltr 命令,如下所示:
//ls -ltr alert_DBA102.log alert_DBA102.log.Z dba102_asmb_12307.trc.Z dba102_asmb_20653.trc.Z//
注意xargs将**尽可能多**的参数附加到命令后面除非制定了__-n 或-s__参数
因此xargs 本身虽然没有多大用处,但在和其他命令相结合时,他的功能非常强大。
下面是另一个示例,我们希望计算这些文件中的行数:
$ file * | grep ASCII | cut -d":" -f1 | xargs wc -l
47853 alert_DBA102.log
19 dba102_cjq0_14493.trc
29053 dba102_mmnl_14497.trc
154 dba102_reco_14491.trc
43 dba102_rvwr_14518.trc
77122 total
(注:上述任务还可用以下命令完成:)
$ wc -l file * | grep ASCII | cut -d":" -f1 | grep ASCII | cut -d":" -f1
该 xargs 版本用于阐释概念。Linux 能用几种方法来完成同一个任务请使用__最适合你__的情况的方法。
使用该方法,你能快速重命名目录中的文件。
//$ ls | xargs -t -i mv {} {}.bak//
__-i__ 选项告诉 xargs 用每项的名称替换 {}。__-t__ 选项指示 xargs 先打印命令,然后再执行。
另一个非常有用的操作是当你使用 vi 打开要编辑的文件时:
//$ file * | grep ASCII | cut -d":" -f1 | xargs vi//
该命令使用 vi 逐个打开文件。当你希望搜索多个文件并打开他们进行编辑时,使用该命令非常方便。
他更有几个选项。最有用的可能是 __-p__ 选项,他使操作具有可交互性:
//$ file * | grep ASCII | cut -d":" -f1 | xargs -p vi//
vi alert_DBA102.log dba102_cjq0_14493.trc dba102_mmnl_14497.trc
dba102_reco_14491.trc dba102_rvwr_14518.trc ?...
此处的 xarg 需求你在运行每个命令之前进行确认。如果你按下 "y",则执行命令。当你对文件进行某些可能有破坏且不可恢复的操作(如删除或覆盖)时,你会发现该选项非常有用。
__-t __选项使用一个周详模式他显示要运行的命令是调试过程中一个非常有帮助的选项。
如果传递给 xargs 的输出为空怎么办?考虑以下命令:
//$ file * | grep SSSSSS | cut -d":" -f1 | xargs -t wc -l//
//wc -l//
// 0//
//$//
在此处,搜索 "SSSSSS" 后没有匹配的内容;因此 xargs 的输入均为空,如第二行所示(由于我们使用 -t 这个周详选项而产生的结果)。虽然这可能会有所帮助,但在某些情况下,如果没有要处理的内容,你可能希望停止 xargs如果是这样能使用__ -r __选项
//$ file * | grep SSSSSS | cut -d":" -f1 | xargs -t -r wc -l//
//$//
如果没有要运行的内容,该命令退出。
假设你希望使用 rm 命令(该命令将作为 xargs 命令的参数删除文件。然而rm 只能接受**有限数量**的参数。如果你的参数列表超出该限制怎么办xargs 的 __-n __选项限制**从标准输入附加到单个命令行的参数个数**。
下面显示了怎么限制每个命令行仅使用两个参数:即使向 xargs ls -ltr 传递五个文件,但每次向 ls -ltr 仅传递两个文件。
//$ file * | grep ASCII | cut -d":" -f1 | xargs -t -n2 ls -ltr //
ls -ltr alert_DBA102.log dba102_cjq0_14493.trc
-rw-r----- 1 oracle dba 738 Aug 10 19:18 dba102_cjq0_14493.trc
-rw-r--r-- 1 oracle dba 2410225 Aug 13 05:31 alert_DBA102.log
ls -ltr dba102_mmnl_14497.trc dba102_reco_14491.trc
-rw-r----- 1 oracle dba 5386163 Aug 10 17:55 dba102_mmnl_14497.trc
-rw-r----- 1 oracle dba 6808 Aug 13 05:21 dba102_reco_14491.trc
ls -ltr dba102_rvwr_14518.trc
-rw-r----- 1 oracle dba 2087 Aug 10 04:30 dba102_rvwr_14518.trc
使用该方法,你能快速重命名目录中的文件。
//$ ls | xargs -t -i mv {} {}.bak//
__-i __选项告诉 xargs 用每项的名称替换 {}。__ -i/L 同时意味着-n1参数__
删除数量比较多的文件
//ls | xargs -n 20 rm -fr//
ls当然是输出所有的文件名(用空格分割). xargs就是将ls的输出每__20个为一组__(以空格为分隔符)作为rm -rf的参数.也就是说将所有文件名20个为一组由rm -rf删除这样就不会超过命令行的长度了