-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmonth_to_number.sh
executable file
·84 lines (69 loc) · 2.21 KB
/
month_to_number.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
#!/usr/bin/env bash
# Script Name: convert_month.sh
# Description: Converts between month names and numbers.
# Usage: convert_month.sh <month|number>
# <month|number> - either a month name or a number
# Example: ./convert_month.sh 7
# ./convert_month.sh jul
number_to_month() {
# Converts a number to the corresponding month name
# $1: an integer number
# Check if exactly one argument is passed
if [ $# -ne 1 ]; then
echo "Error: exactly one argument is required"
return 1
fi
# Check if the argument is an integer
if ! [[ $1 =~ ^[0-9]+$ ]]; then
echo "Error: argument must be an integer"
return 1
fi
# Check if the argument is in the range of months
if [ "$1" -lt 1 ] || [ "$1" -gt 12 ]; then
echo "Error: argument must be in the range of months"
return 1
fi
# Convert the number to the corresponding month
local months=(jan feb mar apr may jun jul aug sep oct nov dec)
echo "${months[$1-1]}"
}
month_to_number() {
# Converts a month name to the corresponding number
# $1: a month name
# Check if exactly one argument is passed
if [ $# -ne 1 ]; then
echo "Error: exactly one argument is required"
return 1
fi
# Check if the argument is a valid month name
if ! [[ $1 =~ ^[a-z]+$ ]]; then
echo "Error: argument must be a month name"
return 1
fi
# Convert the month name to the corresponding number
local months=(jan feb mar apr may jun jul aug sep oct nov dec)
for i in "${!months[@]}"; do
if [ "$1" = "${months[$i]}" ]; then
echo $((i+1))
return 0
fi
done
# If the month name is not found
echo "Error: argument must be a month name"
return 1
}
main() {
if [ $# -ne 1 ]; then
# Exactly one argument is required: either month or number
echo "Usage: $0 <month|number>"
return 1
fi
if [[ $1 =~ ^[0-9]+$ ]]; then
# If the argument is an integer, convert it to the corresponding month
number_to_month "$1"
else
# If the argument is a month name, convert it to the corresponding number
month_to_number "$1"
fi
}
main "$@"