-
Notifications
You must be signed in to change notification settings - Fork 0
/
validint.sh
executable file
·53 lines (45 loc) · 1.4 KB
/
validint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
#validint--Validates inteer input, allowing negative integers too
validint()
{
# Validate first field and test that value against min value $2 and/or
# max value $3 if they are supplied. If the value isn't within range
# or it's not composed of just digits, fail.
number="$1"; min="$2" max="$3"
if [ -z $number ] ; then
echo "You didn't enter anything. Please enter a number. " >&2
return 1
fi
# Is the first character a '-' sign?
if [ "${number%${number#?}}" = "-" ] ; then
testvalue="${number#?}" # grab all but the first character to test
else
testvalue="$number"
fi
# Create a version of the number that has no digits for testing.
nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')"
# Check for nondigit characters.
if [ ! -z $nodigits ] ; then
echo "Invalid number format ! Only Digits, no commas, spaces, etc." >&2
return 1
fi
if [ ! -z $min ] ; then
# Is the input les than the minimum value?
if [ "$number" -lt "$min" ] ; then
echo "Your value is too small: smallest acceptable value is $min. " >&2
return 1
fi
fi
if [ ! -z $max ] ; then
# is the input greater than th emaximu value?
if [ "$number" -gt "$max" ] ; then
echo "Your value is too big: largest acceptable value is $ max." >&2
return 1
fi
fi
return 0
}
#input validation
#if validint "$1" "$2" "$3" ; then
# echo "Input is valid integer within your constraints."
#fi