I am new to bash and writing a script that needs to compare the minor version of the kernel to see if it is greater than or equal to 10, and exit if it is not. Currently I have something like this:
KERNEL=$(uname -r)
declare -i MINOR_VERSION=$(echo $KERNEL | cut -c 3-4)
if [ "10" -gt "$MINOR_VERSION"]; then exit 0; fi
This is bad code, and doesn't work if the minor version is < 10 since I am using cut and depending on it being two digits. What I probably need is something that parses the minor version by using the dots. Example:
$ uname -r
3.4.0-60-generic
$ MNR_VAR=<awesome bash code, with cut or sed or something>
$ echo $MNR_VAR
4
I have been reading cut
and sed
documentation but have just been slow picking it up. I would appreciate the help!
TL;DR - looking for a bash command that will extract an int
surrounded by the first two dots in a variable. "3.13.0.x" returns '13', "3.2.0.x" returns '2', etc.
EDIT: Some answers as one liners below for those curious.
uname -r | cut -d '.' -f2
uname -r | awk -F . '{print $2}'
kernel="$(uname -r)" | tmp="${kernel#*.}" | minor="${tmp%%.*}" | echo "$minor"
The problem is you are using
-c
tocut
. Don't do that.Use the
-f
and-d
flags instead to control the delimiter and fields to output.Or use
awk -F . '{print $2}' <<< "$(uname -r)"
.Or use
IFS=. read -r _ minor _rest <<< "$(uname -r)"; echo "$minor"
(which has the benefit of not using any external utilities).The usage of
<<< "$(uname -r)"
is bash-specific (I believe) but avoids the need for a pipe (|
) and the sub-shell that it involves.