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.
views:
138answers:
2
A:
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:
if [ $x -gt $y ]
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 the test
command, which all the docs I can find seem quite reticent about.
anon
2010-06-03 12:28:28
+2
A:
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 ]
pixelbeat
2010-06-03 14:43:17