xargs命令长度限制 [英] xargs command length limits

查看:59
本文介绍了xargs命令长度限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jsonlint来整理目录中的一堆文件(递归).我写了以下命令:

I am using jsonlint to lint a bunch of files in a directory (recursively). I wrote the following command:

查找./config/pages -name'* .json'-print0 |xargs -0I%sh -c'echo Linting:%;jsonlint -V ./config/schema.json -q%;'

它适用于大多数文件,但某些文件却出现以下错误:

It works for most files but some files I get the following error:

Linting: ./LONG_FILE_NAME.json
fs.js:500
 return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                ^
  Error: ENOENT, no such file or directory '%'

长文件名似乎失败.有没有办法解决这个问题?谢谢.

It appears to fail for long filenames. Is there a way to fix this? Thanks.

修改1:找到了问题.

-我代表

为每个输入行执行实用程序,替换一个或多个事件replstr最多可替换的次数(如果未指定-R标志,则为5)完整输入行的实用程序参数.所结果的参数,替换完成后,将不允许增长超过255个字节;这是通过将尽可能多的尽可能包含replstr的参数,以构造参数实用程序,最多255个字节.255个字节的限制不适用于实用程序的参数,其中不包含replstr,此外,没有替换将在实用程序本身上完成.表示-x.

Execute utility for each input line, replacing one or more occurrences of replstr in up to replacements (or 5 if no -R flag is specified) arguments to utility with the entire line of input. The resulting arguments, after replacement is done, will not be allowed to grow beyond 255 bytes; this is implemented by concatenating as much of the argument containing replstr as possible, to the con-structed arguments to utility, up to 255 bytes. The 255 byte limit does not apply to arguments to utility which do not contain replstr, and furthermore, no replacement will be done on utility itself. Implies -x.

修改2:部分解决方案.支持的文件名比以前更长,但仍然没有我需要的长.

Edit 2: Partial solution. Supports longer file names than before but still not as long as I need.

查找./config/pages -name'* .json'-print0 |xargs -0I%sh -c'文件=%;回声Linting:$ file;jsonlint -V ./config/schema.json -q $ file;'

推荐答案

在类似BSD的系统上(例如Mac OS X)

如果您碰巧是在Mac或freebsd等上,则您的 xargs 实现可能支持选项 -J ,该选项不会受到对选项 -I .

On BSD like systems (e.g. Mac OS X)

If you happen to be on a mac or freebsd etc. your xargs implementation may support option -J which does not suffer from the argument size limits imposed on option -I.

手册页中的内容

-J replstr
If this option is specified, xargs will use the data read from standard input to replace the first occurrence of replstr instead of appending that data after all other arguments. This option will not effect how many arguments will be read from input (-n), or the size of the command(s) xargs will generate (-s). The option just moves where those arguments will be placed in the command(s) that are executed. The replstr must show up as a distinct argument to xargs. It will not be recognized if, for instance, it is in the middle of a quoted string. Furthermore, only the first occurrence of the replstr will be replaced. For example, the following command will copy the list of files and directories which start with an uppercase letter in the current directory to destdir:
/bin/ls -1d [A-Z]* | xargs -J % cp -Rp % destdir

如果您需要多次引用 repstr (*向上点TL; DR -J 仅替换首次出现),则可以使用以下模式:

If you need to refer to the repstr multiple times (*points up* TL;DR -J only replaces first occurrence) you can use this pattern:

echo hi | xargs -J{} sh -c 'arg=$0; echo "$arg $arg"' "{}"
=> hi hi

符合POSIX的方法

符合posix的方法是使用其他一些工具,例如 sed 构造要执行的代码,然后使用 xargs 指定工具.如果在xargs中未使用repl字符串,则255个字节的限制不适用. xargs POSIX规范

POSIX compliant method

The posix compliant method of doing this would be to use some other tool, e.g. sed to construct the code you want to execute and then use xargs to just specify the utility. When no repl string is used in xargs the 255 byte limit does not apply. xargs POSIX spec

find . -type f -name '*.json' -print |
  sed "s_^_-c 'file=\\\"_g;s_\$_\\\"; echo \\\"Definitely over 255 byte script..$(printf "a%.0s" {1..255}): \\\$file\\\"; wc -l \\\"\\\$file\\\"'_g" |
  xargs -L1 sh

这当然大大违背了 xargs 的初衷,但仍然可以用于例如使用 xargs -L1 -P10 sh 进行并行执行,虽然不是posix,但已得到广泛支持.

This of course largely defeats the purpose of xargs to begin with, but can still be used to leverage e.g. parallel execution using xargs -L1 -P10 sh which is quite widely supported, though not posix.

这篇关于xargs命令长度限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆