-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidint
More file actions
executable file
·46 lines (40 loc) · 1.16 KB
/
validint
File metadata and controls
executable file
·46 lines (40 loc) · 1.16 KB
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
#!/bin/bash
# validint 음수인 정수를 허용하면서 정수 입력을 검증
validint()
{
# 첫 번째 필드의 유효성을 검사하고 최솟값 $ 2
# 그리고 / 또는 최대값 $3이 제공되면 해당 값을 테스트한다.
# 값이 범위 내가 아니거나 숫자만으로 구성되지 않은 경우에는 실패한다.
number="$1"; min="$2"; max="$3"
if [ -z $number ] ; then
echo "You didn't enter anything. Please enter a number." >&2
return 1
fi
if [ "${number%${number#?}}" = "-" ] ; then
testvalue="${number#?}"
else
testvalue="$number"
fi
nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')"
if [ ! -z $nodigits ] ; then
echo "Invalid number format! Only digits, no commas, space, etc." >&2
return 1
fi
if [ ! -z $min ] ; then
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
if [ "$number" -gt "$max" ] ; then
echo "Your value is too big: largest acceptable value is $max." >&2
return 1
fi
fi
return 0
}
#
# if validint "$1" "$2" "$3" ; then
# echo "Input is a valid integer within your constraints."
# fi