Find maximum positive integer value in Bourne Shell

后端 未结 3 568
隐瞒了意图╮
隐瞒了意图╮ 2021-01-13 16:41

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 maxi

3条回答
  •  日久生厌
    2021-01-13 17:19

    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 ]
    

提交回复
热议问题