在 Bourne Shell 中查找最大正整数值
我正在循环中检查计数器,以确定它是否大于某个最大值(如果在可选参数中指定)。由于它是可选的,我可以将最大值默认为特殊值或最大可能的整数。第一个选项需要在每次迭代时进行额外检查,因此我想找出与 -gt
Bourne Shell 操作一起使用的最大整数是多少 。
I'm checking a counter in a loop to determine if it's larger than some maximum, if specified in an optional parameter. Since it's optional, I can either default the maximum to a special value or to the maximum possible integer. The first option would require an extra check at each iteration, so I'd like to instead find out what is the maximum integer that will work with the -gt
Bourne Shell operation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会避开整数限制,因为它们是不可移植的并且有问题
相反,我只是按照您的建议进行操作并进行额外的比较,例如:
I'd stay clear of integer limits as they're non portable and problematic
Instead I'd just do as you suggest and do the extra comparison like:
在我的系统上,Bash 的最大整数似乎与我的 Perl POSIX 库的 LONG_MAX 常量相同。显然,这会因您的平台以及 Bash 的编译方式等而异。但这似乎是测试它的一个很好的起点:
更新:在旧的 32 位 Linux 上尝试此操作后,我看到我的 Perl 的 POSIX LONG_MAX是 2147483647,但是 Bash 仍然有相同的限制。它似乎是在 /usr/include/limits.h 中定义的,并且取决于您的 __WORDSIZE,即使在 32 位系统上,它也可能是 64 位:
On my system, the maximum integer of Bash seems to be the same as the LONG_MAX constant of my Perl POSIX library. Obviously, this will vary on your platform, and how your Bash was compiled, etc. But that seems to be a good starting point for testing it:
Update: After trying this on an old 32 bit Linux, I see that my Perl's POSIX LONG_MAX is 2147483647, but that Bash still has the same limit. It seems to be defined in /usr/include/limits.h, and to depend on your __WORDSIZE, which may be 64 bits even on 32 bit systems :
Bourne shell 没有用于存储或操作数字的工具 - 一切都存储为字符串。如果您询问此类事情:
那么这是由一个名为
test
的单独(在 Bourne shell 中)可执行文件处理的,它有一个名为“[”的符号链接。所以你的问题实际上是关于 test 命令的限制,我能找到的所有文档似乎对此都很沉默。The Bourne shell has no facilities for storing or manipulating numbers - everything is stored as a string. If you are asking about this kind of thing:
then that is handled by a separate (in the Bourne shell) executable called
test
, which has a symbolic link called '['. So your question is really about the limits of thetest
command, which all the docs I can find seem quite reticent about.