这是我当前使用的命令:
xargs -n 1 -P 100 php b <links
我有一个脚本“b”和一个在“links”中包含链接的文件,有时我不知道为什么它不能正常工作并添加到每一行符号“?” 像这样:
root 19714 0.0 1.0 19880 5480 pts/2 R+ 11:19 0:00 php b http://www.google.com?
root 19715 0.0 0.9 19524 4892 pts/2 R+ 11:19 0:00 php b http://www.alexa.com?
root 19716 0.0 1.0 19880 5496 pts/2 R+ 11:19 0:00 php b http://www.amazon.com?
看到 ps-aux 结果行的末尾是“?”符号,而我的文件不包含该符号...但并非总是如此...可能是什么问题?
最佳答案
将评论转换为答案
您的链接文件是否可以有 CRLF(Windows 或 DOS 样式)行结尾?在这种情况下,?
代表 CR(回车)……解决方法是在该行的某个位置将 DOS 文件转换为 Unix 文件。例如:
tr -d '\r' < links | xargs -n 1 -P 100 php b
I build the links file with
cat "[another file]" | sort | uniq > links
. So I don't think it is a Windows/DOS style issue.
是[another file]
有 DOS 格式的行结尾吗?如果是这样,links
也会;如果输入中存在 CRLF 行结尾,命令行将保留它们。
此外,使用cat
通常是不必要的(UUoC或猫的无用使用),在这种情况下uniq
也不需要;你可以使用
sort -u "[another file]" > links
[another file]
is like this:line1\r\nline2\r\nline3\r\n
\r\n
是 Unix 上报告 CRLF 行结尾的方式之一。 \r
C 和其他语言中用于回车符 CR 的表示法; \n
符号用于换行符(NL,又名换行或 LF)。
BTW: If I paste the same file with copy/paste via
vi
ornano
it works.
编辑器可能会自动为您转换文件格式。请参阅:How to convert the ^M linebreak to normal linebreak in a file opened in vim?或者,如果您正在复制和粘贴,则副本很可能看不到,因此也不会复制 CR 字符。
I tried with
tr -d …
and it works.
好;这至少意味着这是一个合理的解释,而且一切都有道理。如果您不需要单独的链接列表,您可以使用:
sort -u "[another file]" | tr -d '\r' | xargs -n 1 -P 100 php b
这一切都在一个命令行中完成,无需清理中间文件。您可以添加tee links |
之前xargs
当然,如果您也想要文件列表。
关于unix - xargs 无法正确逐行读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28834221/