Skip to content

Commit 3654121

Browse files
authored
Merge pull request #106 from redhat-performance/detect_os/major_minor
Add Major/Minor version detection to detect_os
2 parents 49de877 + eabfcde commit 3654121

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

detect_os

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
os_release_file="/etc/os-release"
2121
get_version=0 # Fetch OS name by default
22+
get_major=0
23+
get_minor=0
24+
vers_sep="."
2225

2326
usage()
2427
{
@@ -29,6 +32,16 @@ usage()
2932
echo -e "\t --os-version"
3033
echo -e "\t\t Determine the current OS version"
3134

35+
echo -e "\t --major-version"
36+
echo -e "\t\t Output the major version number of the OS (requires --os-version)"
37+
38+
echo -e "\t --minor-version"
39+
echo -e "\t\t Output the minor version number of the OS (requires --os-version)"
40+
41+
echo -e "\t --version-separator"
42+
echo -e "\t\t Set the separator between the major and minor version numbers"
43+
echo -e "\t\t Default is \".\""
44+
3245
echo -e "\t -h/--help/--usage"
3346
echo -e "\t\t Displays this message"
3447
}
@@ -41,23 +54,48 @@ echo_stderr()
4154
parse_os_release()
4255
{
4356
source $os_release_file
57+
has_major_minor=0
4458
if [[ $get_version -eq 1 ]]; then
45-
echo $VERSION_ID
59+
if [[ "$VERSION_ID" == *"${vers_sep}"* ]]; then
60+
major_version=$(echo $VERSION_ID | cut -d${vers_sep} -f1)
61+
minor_version=$(echo $VERSION_ID | cut -d${vers_sep} -f2)
62+
else # Does not have a major/minor structure (using the provided separator)
63+
major_version=$VERSION_ID
64+
minor_version=""
65+
vers_sep=""
66+
fi
67+
68+
if [[ $get_major -eq 1 ]]; then
69+
echo $major_version
70+
fi
71+
if [[ $get_minor -eq 1 ]] && [[ -n "$minor_version" ]];then
72+
echo $minor_version
73+
fi
74+
if [[ $get_major -eq 0 ]] && [[ $get_minor -eq 0 ]]; then
75+
echo $VERSION_ID
76+
fi
4677
else
4778
echo $ID
4879
fi
4980
}
5081

82+
ARG_OPTS=(
83+
"version-separator"
84+
)
85+
5186
NOARG_OPTS=(
5287
"h"
5388
"help"
54-
5589
"usage"
90+
5691
"os-version"
92+
"major-version"
93+
"minor-version"
5794
)
5895

5996
opts=$(getopt \
6097
--longoptions "$(printf "%s," "${NOARG_OPTS[@]}")" \
98+
--longoptions "$(printf "%s:," "${ARG_OPTS[@]}")" \
6199
--name "$(basename "$0")" \
62100
--options "hc:" \
63101
-- "$@"
@@ -81,6 +119,18 @@ while [[ $# -gt 0 ]]; do
81119
get_version=1
82120
shift 1
83121
;;
122+
--major-version)
123+
get_major=1
124+
shift 1
125+
;;
126+
--minor-version)
127+
get_minor=1
128+
shift 1
129+
;;
130+
--version-separator)
131+
vers_sep=$2
132+
shift 2
133+
;;
84134
--)
85135
break
86136
;;

0 commit comments

Comments
 (0)