这:
#!/bin/bash
# Run command ~100Kbytes long
/bin/echo $(perl -e 'print "x"x100000') | wc
# Run command ~54Kbytes long
# This line fails: line 7: /bin/echo: Argument list too long
/bin/echo $(perl -e 'print "x "x27000') | wc
# Same command, but run using xargs
# Run command ~100Kbytes long
perl -e 'print "x"x100000' | xargs -n 100000 /bin/echo | wc
# Run command ~54Kbytes long
# This line fails: xargs: /bin/echo: Argument list too long
perl -e 'print "x "x27000' | xargs -n 100000 /bin/echo | wc
在 GNU/Linux 上工作正常,但 2 54Kbytes 行在 MacOS X 上失败。
ARG_MAX
远高于 100KBytes,请注意 100Kbytes 行确实不是失败 - 54KBytes 线路失败。
mac$ getconf ARG_MAX
262144
mac$ uname -a
Darwin macosx 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:26:45 PDT 2012; root:xnu-1699.32.7~1/RELEASE_I386 i386
# Kusalananda suggests it may be due to the size of the environment
mac$ env | wc
27 32 956
为什么 54Kbytes 命令失败?
有没有一种方法可以在不运行它的情况下预测参数列表对于 MacOS X 来说是否太长?
研究
这:
#!/bin/bash
runtest() {
echo environment size:
env | wc
echo Run command ~100Kbytes long
/bin/echo $(perl -e 'print "x"x100000') | wc
echo Run command ~54Kbytes long
# This line fails: line 7: /bin/echo: Argument list too long
/bin/echo $(perl -e 'print "x "x27000') | wc
# Same command, but run using xargs
echo Run command ~100Kbytes long
perl -e 'print "x"x100000' | xargs -n 100000 /bin/echo | wc
echo Run command ~54Kbytes long
# This line fails: xargs: /bin/echo: Argument list too long
perl -e 'print "x "x27000' | xargs -n 100000 /bin/echo | wc
echo
}
# Clean environment
runtest
# Make a huge environment
for a in `seq 5000`; do eval "a$a=1" ; done
for a in `seq 5000`; do eval "a$a() { 1; }" ; done
# This works as before
runtest
# Export environment
for a in `seq 5000`; do eval export a$a ; done
for a in `seq 5000`; do eval export -f a$a ; done
# Now the 100Kbytes commands fail, too
runtest
给出这个输出:
environment size:
6027 6032 47849
Run command ~100Kbytes long
1 1 100001
Run command ~54Kbytes long
test: line 10: /bin/echo: Argument list too long
0 0 0
Run command ~100Kbytes long
1 1 100001
Run command ~54Kbytes long
xargs: /bin/echo: Argument list too long
0 0 0
environment size:
6027 6032 47849
Run command ~100Kbytes long
1 1 100001
Run command ~54Kbytes long
test: line 10: /bin/echo: Argument list too long
0 0 0
Run command ~100Kbytes long
1 1 100001
Run command ~54Kbytes long
xargs: /bin/echo: Argument list too long
0 0 0
environment size:
16027 26032 126742
Run command ~100Kbytes long
test: line 7: /bin/echo: Argument list too long
0 0 0
Run command ~54Kbytes long
test: line 10: /bin/echo: Argument list too long
0 0 0
Run command ~100Kbytes long
xargs: insufficient space for argument
0 0 0
Run command ~54Kbytes long
xargs: /bin/echo: Argument list too long
0 0 0
所以,库萨罗南达是对的出口环境可以产生影响。目前还不清楚计算这个的公式是什么:也许纯粹是大小?也许变量的数量也很重要?也许只是名字的长度?也许是这些的线性组合?
它仍然没有解释在给定环境中 100Kbytes 命令可以正常工作,但 54Kbytes 命令则不能。
就好像 MacOS 不仅对总大小有限制,而且对参数数量也有限制。
如果 MacOS 每个参数使用额外的 8 个字节,这些数字也有意义:
# One big argument
100K * "x" = 100000+2 < 262144 # Works
# 27K small arguments
27K * "x " = 27K*(8+2) > 262144 # Fails
# 26K small arguments
26K * "x " = 26K*(8+2) < 262144 # Works
但 MacOS 能做到这一点吗?
答案1
进一步研究表明(MacOS 版本未知):
Effective length =
length of arguments +
5 * number of arguments +
length of body/value of exported functions/variables +
length of names of exported functions/variables +
4 * number of exported functions/variables
如果有效长度 < 256 KB,则命令将运行。目前尚不清楚这是否适用于所有版本的 MacOS。
对于 MacOS El Capitan 10.11.4,这给出了悲观命令行长度(假设您要运行的命令是/bin/echo x x x x ...
):
perl -e '
$envc=(keys %ENV);
$envn=length join"",(keys %ENV);
$envv=length join"",(values %ENV);
$maxlen=3+(262144 - $envn - $envv) / 5 - $envc*2;
print("Max len = $maxlen\n");
'
目前尚不清楚这是否适用于所有版本的 MacOS。