-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.sh
145 lines (130 loc) · 2.14 KB
/
library.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function validint
{
number="$1"
min="$2"
max="$3"
if [ -z $number ]
then
echo "You didn't provide anything, please provide a number [min] [max]" >&2
return 1
fi
if [ $(echo "$number" | cut -c1) = '-' ]
then
testnumber="$(echo "$number" | cut -c2-)"
else
testnumber="$number"
fi
if [ ! -z $(echo "$testnumber" | sed 's/[[:digit:]]//g') ]
then
echo "Usage $0: Not a valid number, only digits allowed" >&2
return 1
fi
if [ ! -z "$min" ]
then
if [ "$number" -lt "$min" ]
then
echo "The number provided is too short, min value allowed is: $min" >&2
return 1
fi
fi
if [ ! -z "$max" ]
then
if [ "$number" -gt "$max" ]
then
echo "The number provided is too big, max value allowed is: $max" >&2
return 1
fi
fi
return 0
}
function validFloat
{
float="$1"
if [ ! -z $(echo $float | sed 's/[^.]//g' ) ]
then
decimal="$(echo $float | cut -d. -f1)"
fractional="$(echo $float | cut -d. -f2)"
if [ ! -z "$decimal" ]
then
if ! validint "$decimal" "" ""
then
return 1
fi
fi
if [ ! -z "$fractional" ]
then
if [ $(echo $fractional | cut -c1) = "-" ]
then
return 1
else
if ! validint "$fractional" "" ""
then
return 1
fi
fi
else
return 1
fi
else
if [ "$float" = "-" ]
then
return 1
else
if ! validint "$float" "" ""
then
return 1
fi
fi
fi
return 0
}
function isLeapYear
{
#return 0 if it is a leap year
# 1 otherwise
year=$1
if [ $((year%4)) -ne 0 ]; then
return 1
elif [ $((year%400)) -eq 0 ]; then
return 0
elif [ $((year%100)) -eq 0 ];then
return 1
else
return 0
fi
}
function in_path()
{
cmd=$1
ourpath=$2
result=1
oldIFS=$IFS
IFS=":"
for directory in $ourpath
do
if [ -x $directory/$cmd ]
then
result=0
fi
done
IFS=$oldIFS
return $result
}
function checkForCmdInPath()
{
var="$1"
if [ "$var" != "" ]
then
if [ "${var:0:1}" = "/" ]
then
if [ ! -x $var ]
then
return 1
fi
elif ! in_path $var "$PATH"
then
return 2
fi
fi
}
name="Aldo Aranda"