在 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 ]