为什么我会和QUOT; / bin / sh的:参数列表过长"当传递引用参数呢? [英] Why do I get "/bin/sh: Argument list too long" when passing quoted arguments?

查看:324
本文介绍了为什么我会和QUOT; / bin / sh的:参数列表过长"当传递引用参数呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多久可以是可以传递给上海-c'' A命令行? (在bash和Bourne shell的)

How long can be a command line that can be passed to sh -c ''? (in bash and in bourne shell)

的限制比从OS(在现代Linux的情况下)低得多。

The limit is much lower than that from the OS (in case of modern Linux).

例如:

$ /bin/true $(seq 1 100000)
$ /bin/sh -c "/bin/true $(seq 1 100000)"
bash: /bin/sh: Argument list too long

和我怎么能解决这个问题?

And how could I circumvent this problem?

更新

我要指出,在getconf 不禁在这里(因为这不是系统限制):

I want to note that getconf can't help here (because that is not a system limit):

$ seq 1 100000 | wc -c
588895
$ getconf ARG_MAX
2097152

更新#2

现在我已经理解有什么意义在这里。这是不是一个外壳的限制,这是一个系统限制,但每个参数的长度,而不是整个arglist中。

Now I've understood what is the point here. That is not a shell limit, that is a system limit but for the length of each argument, not for the entire arglist.

$ /bin/true $(seq 1 100000)
$ /bin/true "$(seq 1 100000)"
bash: /bin/true: Argument list too long

感谢您,codeGnome,您的解释。

Thank you, CodeGnome, for the explanation.

推荐答案

一个单个参数必须比MAX_ARG_STRLEN短。

TL;DR

A single argument must be shorter than MAX_ARG_STRLEN.

根据此链接

和附加限制2.6.23以来,一种说法不能长于MAX_ARG_STRLEN(131072)。如果你生成一个长调用等。

And as additional limit since 2.6.23, one argument must not be longer than MAX_ARG_STRLEN (131072). This might become relevant if you generate a long call like "sh -c 'generated with long arguments'".

这正是问题的OP标识。虽然允许的可能相当大参数的数量(见在getconf ARG_MAX ),当你通过一个引用命令的 / bin / sh的的外壳间$ p $点引用的命令作为单个字符串。在OP的例子,它是超出了MAX_ARG_STRLEN限制,而不是扩展的参数列表的长度这个单一字符串。

This is exactly the "problem" identified by the OP. While the number of arguments allowed may be quite large (see getconf ARG_MAX), when you pass a quoted command to /bin/sh the shell interprets the quoted command as a single string. In the OP's example, it is this single string that exceeds the MAX_ARG_STRLEN limit, not the length of the expanded argument list.

参数限值实施的具体。但是,这篇Linux Journal中推荐了几种方法来解决它们,其中包括提高了系统的限制。这可能无法直接适用到OP,但在一般情况下仍然是有用的。

Argument limits are implementation specific. However, this Linux Journal article suggests several ways to work around them, including increasing system limits. This may not be directly applicable to the OP, but it nonetheless useful in the general case.

该任择议定书的问题实际上不是一个真正的问题。现在的问题是气势不解决现实世界的问题任意约束。

The OP's issue isn't actually a real problem. The question is imposing an arbitrary constraint that doesn't solve a real-world problem.

您可以通过使用循环解决这个很轻松了。例如,对于击4:

You can work around this easily enough by using loops. For example, with Bash 4:

for i in {1..100000}; do /bin/sh -c "/bin/true $i"; done

工作得很好。这肯定将是缓慢的,因为你生成一个过程的每个通过循环,但它肯定在你身边所遇到的命令行的限制得到。

works just fine. It will certainly be slow, since you're spawning a process on each pass through the loop, but it certainly gets around the command-line limit you're experiencing.

如果一个循环无法解决您的问题,请更新来形容你实际尝试使用很长的参数列表来解决问题的问题。探索独断专行长度限制是一个学术活动,而不是-话题堆栈溢出。

If a loop doesn't resolve your issue, please update the question to describe the problem you're actually trying to solve using really long argument lists. Exploring arbitrary line-length limits is an academic exercise, and not on-topic for Stack Overflow.

这篇关于为什么我会和QUOT; / bin / sh的:参数列表过长"当传递引用参数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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