xargs的默认分隔符可以更改吗? [英] Can xargs' default delimiter be changed?
问题描述
我想要以下行为,而不必用选项明确指定它:
I want the following behavior without having to explicitly specify it with options:
xargs -d '\n'
与大多数命令不同,您不能仅使用别名,因为管道无法识别别名(如旁注,为什么要这样设计?)。我也尝试过创建自己的〜/ bin / xargs
脚本,但我认为它不像在脚本中读取字符串那样简单地读取 $ @。
Unlike with most commands, you can't just use an alias because pipes don't recognize aliases (as a side-note, why is it designed this way?). I also tried creating my own ~/bin/xargs
script but I think it's not as simple as reading "$@" as a string inside the script.
任何建议如何使分隔符默认为换行符?当路径中有空格时,我不想出现很多错误(并且使用 find ... -print0 | xargs -0
还有其他不良影响) 。
Any suggestions how to make the delimiter a newline by default? I don't want to get a bunch of errors when I have a space in the path (and using find ... -print0 | xargs -0
has other unwanted effects).
更新
我的shell脚本尝试是这样的:
My shell script attempt is this:
/usr/local/bin/xargs -d '\n' "$@"
推荐答案
我测试了 xargs -d'\n'-n1 echo
在命令行上,并且按宣传的方式工作;
I tested xargs -d '\n' -n1 echo
on the command line and it worked as advertised; breaking the input up into single lines and echoing it.
您可能会因变量分配过程中外壳转义的假设而感到困惑。例如,如果您的Shell脚本与此类似:
You could be being bitten by assumptions regarding shell escaping during variable assignment. For instance, if your shell script is similar to this:
CMD = xargs -d'\n'-n1 echo
cat输入文件| $ CMD
CMD="xargs -d '\n' -n1 echo"
cat inputfile | $CMD
然后会发生错误,因为整个CMD字符串周围的双引号保留了单引号和反斜杠分隔符。
then an error occurs, because the double-quotes around the entire CMD string preserve both the single-quotes and the backslash in the delimiter.
如果将xargs命令括在引号中,则可以将其更改为以下内容(无单引号)
If you are enclosing the xargs command within quotes, you could change it to the following (no single-quotes)
CMD = xargs -d \n -n1 echo
cat输入文件| $ CMD
CMD="xargs -d \n -n1 echo"
cat inputfile | $CMD
,它可能会根据您的需要起作用。
and it will likely function as you need.
经过测试在CentOS 6.4,xargs版本4.4.2
Tested on CentOS 6.4, xargs version 4.4.2
这篇关于xargs的默认分隔符可以更改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!