在 Bourne Shell 中查找最大正整数值 [英] Find maximum positive integer value in Bourne Shell

查看:61
本文介绍了在 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.

推荐答案

我会避开整数限制,因为它们不可移植且有问题

I'd stay clear of integer limits as they're non portable and problematic

$ test 123412341234112341235 -gt 1 || echo bash compares ints
-bash: test: 123412341234112341235: integer expression expected
bash compares ints
$ env test 1 -gt 123412341234112341235 || echo coreutils compares strings
coreutils compares strings

相反,我只是按照您的建议进行操作并进行额外的比较,例如:

Instead I'd just do as you suggest and do the extra comparison like:

[ "$limit" ] && [ $count -gt $limit ]

这篇关于在 Bourne Shell 中查找最大正整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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