perl 中的系统命令行最大长度 [英] system command line max length in perl

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

问题描述

为了减少发出的命令数量,我需要在perl语句中放入尽可能多的字符

To reduce the number of commands issued, I need to put as many characters as I can in the perl statement

system("$cmd_and_parameters")

在 Windows 和 Linux 下,我可以假设的(安全)最大行长度是多少?

我的问题是由于需要发出 Oetikerrrdtool 套件"的命令,不使用正确的 perl 模块(请不要为此责怪我).

My question is due to the need to issue commands of the Oetiker "rrdtool suite", without using the proper perl modules (please, don't blame me for this).

我的第一次尝试是使用前面提到的功能,设置

My first attempt was using the before mentioned function, setting

$cmd_and_parameters = "path_to_rrdtool/rrdtool update $db $timestamp:$value" 

它有效,但考虑到我必须执行的更新数量,它很慢.

It works, but it's slow, considering the number of updates I have to do.

因此,由于rrdtool update"允许,我决定将参数数量增加到允许的最大值,从而减少系统"调用的数量.

So, as "rrdtool update" allows it, I decided to increase the number of arguments up to the maximum allowed, reducing the number of "system" calls.

使用合理的少量参数,它工作得更好,无论如何它足以满足我的需要,但仍然存在问题我可以在系统函数中放入多少 $timestamp:$value 对?

Using a reasonable small number of arguments, it works much better, and anyway it's good enough to my needs, but still remains the question "How many $timestamp:$value couples can I put in the system function?

也就是说:一个perl系统函数可以有多少个字符?

That is to say: how many characters can be in a perl system function?

推荐答案

在 Linux 上,限制为 ARG_MAX(您可以使用 getconf ARG_MAX 查询).我有编辑内核限制的习惯(例如在 linux-3.7.1/include/uapi/linux/limits.h 中增加它).

On Linux, the limit is ARG_MAX (which you could query with getconf ARG_MAX). I have th habit of editing my kernel's limit (e.g. in linux-3.7.1/include/uapi/linux/limits.h to increase it).

(注意 setrlimit(2) RLIMIT_STACK 实际上可能会降低程序参数的最大大小)

(Notice that setrlimit(2) RLIMIT_STACK may in practice lower the maximal size of program arguments)

某些 Linux 内核(特别是旧内核或嵌入式内核)具有下限.你应该准备好让它低至 32768(在我自己的内核上,我将它提高到 2097152).

Some Linux kernels (notably old, or embedded) have a lower limit. You should be prepared to have it as low as perhaps 32768 (on my own kernel I'm raising it to 2097152).

我提高 ARG_MAX 的原因是我有足够的 RAM (16Gbytes),我的 shell 是 zsh 并且我喜欢输入 **/*.c 当我想要(从而避免 find)

The reason I am raising my ARG_MAX is that I have plenty of RAM (16Gbytes), my shell is zsh and I love typing **/*.c when I want to (thus avoiding a find)

阅读 execve(2) Linux 手册页和 Posix exec 文档.Linux 手册页有几段关于参数和环境大小的限制.

Read the execve(2) Linux man page and the Posix exec documentation. The Linux man page has several paragraphs about Limits on size of arguments and environment.

在 Windows(或至少在 MS-DOS 上)它可能低至 128(扩展前).

On Windows (or at least on MS-DOS) it might be as low as 128 (before expansion).

你应该明白参数扩展是(在 Posix 和 Linux 系统上)由 shell 完成的,并且 system(3) 和 Perl 的那个名字的函数可能是启动一个 /bin/sh -c 来进行扩展.重要的是扩展参数列表的大小:所以如果你调用 system("ls */*/*.c") 并且如果 */*/*.c> 即使未扩展的命令行 ls */*/*.c 很短,也可以扩展到一百万个您遇到麻烦的参数(至少在 Posix 和 Linux 上).

And you should understand that argument expansion is (on Posix & Linux system) done by the shell, and that system(3) and Perl's function of that name probably is starting a /bin/sh -c which will do the expansion. What matters is the size of the expanded argument list: so if you call system("ls */*/*.c") and if */*/*.c expands to a million arguments you are in trouble (at least on Posix and Linux) even if the unexpanded command line ls */*/*.c is very short.

传闻在 Windows 和 MS-DOS 上,运行时库将命令行扩展为参数列表.听说程序启动时没有解释终端命令,还有一些运行时启动的东西在做扩展.

On Windows and MS-DOS the runtime library is rumored to expand the command line into an argument list. I heard that the program starts with the terminal command uninterpreted, and some runtime startup thing is doing the expansion.

所以回答你的问题:在 Posix 系统上,重要的是扩展参数列表,如果扩展参数列表小于 32K 字节,你很可能非常安全(实际上 128Kbyte 就足够了).在 Windows 或 MS-DOS(我猜是这样)上,您可能需要将未扩展的命令行限制为 128 个字符,并将其扩展限制为 64Kbytes(实际上更大的阈值通常会起作用).我真的认为你应该总是测试system

So to answer your question: on Posix systems, what matters is the expanded argument list, and if that expanded argument list is smaller than 32K bytes you are very probably very safe (In practice 128Kbyte is good enough). On Windows or MS-DOS (I am guessing that) you probably need to limit the unexpanded command line to 128 chars and its expansion to 64Kbytes (in practice bigger thresholds would often work). I really think you should always test the result of system

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

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