bash ksh 变量接受的最大字符数是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14371326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:16:16  来源:igfitidea点击:

What is the maximum number of characters that the ksh variable accepts?

bashshellunixksh

提问by javapadawan

I am trying to load and parse a really large text file. Although the loading is not a problem, but there are particular lines that have 2908778 characters on a single line.

我正在尝试加载和解析一个非常大的文本文件。虽然加载没有问题,但是有一些特定的行单行有 2908778 个字符。

This is causing an error in my script.

这导致我的脚本出错。

On the script below, I removed all logic and just got straight to read line. I also removed all valid lines and just left the really long line in one text file. When running I get the below error :

在下面的脚本中,我删除了所有逻辑并直接阅读了一行。我还删除了所有有效的行,只在一个文本文件中留下了很长的一行。运行时出现以下错误:

$ dowhiledebug.sh dump.txt
dowhiledebug.sh[6]: no space
Script Ended dump.txt

The actual script:

实际脚本:

 #!/bin/sh
 filename=
 count=1
 if [ -f ${filename} ]; then
    echo "after then"
    while read line;
            do
            echo "$count"
            count=$((count+1))
            done < $filename
 else
    echo "Could not open file $filename"
 fi
 echo "Script Ended $filename"

Updated (2013-01-17)

更新 (2013-01-17)

Follow up question : Is it possible to increase the maximum number of characters that ksh variable accepts?

后续问题:是否可以增加 ksh 变量接受的最大字符数?

采纳答案by David W.

The limit for any shell is the limit of the C command line maximum. Here's a little program that pulls the information out of /usr/include/limits.hfor you:

任何 shell 的限制都是 C 命令行最大值的限制。这是一个小程序,可以/usr/include/limits.h为您提取信息:

cpp <<HERE | tail -1
#include <limits.h>
ARG_MAX
HERE

Mine gives me (256 * 1024) or 262144 characters.

我的给了我 (256 * 1024) 或 262144 个字符。

Doesn't work if the C compiler isn't installed, but it's probably a similar limit.

如果未安装 C 编译器,则不起作用,但这可能是类似的限制。

回答by shellter

what OS and version of ksh? Can you echo ${.sh.version}and get a value? If so, please include in your question above. Or could this be pdksh?

什么操作系统和版本的ksh?你能echo ${.sh.version}得到一个值吗?如果是这样,请包括在您上面的问题中。或者这可能是 pdksh?

Here's a test that will get you in the ballpark, assuming a modern ksh supporting (( i++ ))math evaluations:

假设现代 ksh 支持(( i++ ))数学评估,这里有一个测试可以让您进入球场:

#100 char var
var=1234578901234456789012345678901234567890123456789012345789012344567890123456789012345678901234567890

$ while (( i++ < 10000 )) ;do  var="$var$var" ; print "i=$i\t" ${#var} ; done
i=1      200
i=2      400
i=3      800
i=4      1600
i=5      3200
i=6      6400
i=7      12800
i=8      25600
i=9      51200
i=10     102400
i=11     204800
i=12     409600
i=13     819200
i=14     1638400
i=15     3276800
i=16     6553600
i=17     13107200
i=18     26214400
i=19     52428800
i=20     104857600
i=21     209715200
i=22     419430400
-ksh: out of memory

$ print -- ${.sh.version}
Version JM 93t+ 2010-05-24

AND that is just the overall size of the environment that can be supported. When dealing with the command-line environment and "words" after the program name, there is a limit to the number of words, regardless of overall size.

而且这只是可以支持的环境的整体大小。在处理命令行环境和程序名后的“单词”时,无论整体大小如何,单词的数量都有限制。

Some shells man page will have a section LIMITS that may show something like max-bytes 200MB, max-args 2048. This information may be in a different section, it will definitely have different labels and different values I have included, OR it may not be there at all, hence the above code loop, so look carefully around and if you find a source for this info, either add an answer to this Q, or update this one.

某些 shell 手册页会有一个 LIMITS 部分,可能会显示类似max-bytes 200MB, max-args 2048. 此信息可能位于不同的部分,它肯定会有不同的标签和我包含的不同值,或者它可能根本不存在,因此上面的代码循环,因此请仔细查看,如果您找到此信息的来源,要么为这个问题添加一个答案,要么更新这个问题。

The bash 4.4std man page doesn't seem to have this information and its harder to find a kshdoc all the time. Check your man kshand hope that you can find a documented limit.

bash 4.4STD手册页似乎不具备此信息及其很难找到一个ksh文档的所有时间。检查您的man ksh并希望您能找到记录在案的限制。

IHTH

IHTH