-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserlib.sh
More file actions
173 lines (161 loc) · 3.64 KB
/
userlib.sh
File metadata and controls
173 lines (161 loc) · 3.64 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
# This file contains a few functions useful when included in other scripts.
# Returns the number of days in the month.
# Uses the current month of the current year by default.
# Params $1 - the month
# $2 - the year
daysInMonth() {
# Validate inputs.
month=$(date +%m)
if [ -n "$1" ]; then
if [ $1 -ge 1 -o $1 -le 12 ]; then
month=$1
fi
fi
year=$(date +%Y)
if [ -n "$2" ]; then
echo $2 | grep -q "^[0-9]*$"
if [ $? -eq 0 ]; then
year=$2
fi
fi
# Pad the month to two digits, e.g. 01 to 09.
if [ ${#month} -lt 2 ]; then
month=$(printf '%02d' $month)
fi
# Get the number of days in the month.
case $month in
01 )
days=31
;;
02 )
isLeapYear $year
leap=$?
if [ $leap -eq 0 ]; then
days=29
else
days=28
fi
;;
03 )
days=31
;;
04 )
days=30
;;
05 )
days=31
;;
06 )
days=30
;;
07 )
days=31
;;
08 )
days=31
;;
09 )
days=30
;;
10 )
days=31
;;
11 )
days=30
;;
12 )
days=31
;;
esac
return $days
}
# Checks whether given year is a leap year or not.
# If the year is not supplied, it takes current year as default.
#
# This script assumes that a leap year comes every 4 years,
# but not every 100 years, then again every 400 years.
isLeapYear() {
year=$(date +%Y)
if [ -n "$1" ]; then
echo $1 | grep -q "^[0-9]*$"
if [ $? -eq 0 ]; then
year=$1
fi
fi
mod4=$(($year % 4))
mod100=$(($year % 100))
mod400=$(($year % 400))
if [ $mod4 -ne 0 ]; then
#echo "$year - NOT a leap year"
return 1
elif [ $mod100 -ne 0 ]; then
#echo "$year - IS a leap year"
return 0
elif [ $mod400 -ne 0 ]; then
#echo "$year - NOT a leap year"
return 1
else
#echo "$year - IS a leap year"
return 0
fi
}
# Returns the number of occurrences of a particular day in a month.
# Uses the current day of the current month of the current year by default.
# Params $1 - the day
# $2 - the month
# $3 - the year
numDayInMonth() {
# Validate inputs.
echo $1 | grep -q "^Mo\|Tu\|We\|Th\|Fr\|Sa\|Su$"
if [ $? -ne 0 ]; then
day=$(date +%u)
if [ -n "$1" ]; then
if [ $1 -ge 1 -o $1 -le 7 ]; then
day=$1
fi
fi
else
day=$1
fi
month=$(date +%m)
if [ -n "$2" ]; then
if [ $2 -ge 1 -o $2 -le 12 ]; then
month=$2
fi
fi
year=$(date +%Y)
if [ -n "$3" ]; then
echo $3 | grep -q "^[0-9]*$"
if [ $? -eq 0 ]; then
year=$3
fi
fi
# Make sure the day is in its abbreviated form.
case $day in
1)
day=Mo
;;
2)
day=Tu
;;
3)
day=We
;;
4)
day=Th
;;
5)
day=Fr
;;
6)
day=Sa
;;
7)
day=Su
;;
esac
# Get the calendar output for the month and year, then get the day row and count the columns, minus the row header.
days_in_month=$(($(ncal -d ${year}-${month} | grep $day | wc -w) - 1))
return $days_in_month
}