#5 Validating Integer Input

As you saw in Script #2, validating integer input seems like a breeze until you want to ensure that negative values are acceptable too. The problem is that each numeric value can have only one negative sign, which must come at the very beginning of the value. The validation routine in this script makes sure that negative numbers are correctly formatted, and, to make it more generally useful, it can also check that values are within a range specified by the user.

The Code

 #!/bin/sh # validint -- Validates integer input, allowing negative ints too. function validint { # Validate first field. Then test against min value $2 and/or # max value $3 if they are supplied. If they are not supplied, skip these tests. number="$1"; ...

Get Wicked Cool Shell Scripts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.