Reputation: 129
I create a script that I need the current kernel version in a specific way.
For example, if I use : 3.10.34-1-MANJARO
I want to get only 310
Which is the best/easy way to do it ?
Upvotes: 1
Views: 1284
Reputation: 24
A 'Bash only' solution:
declare -a TEMP2
TEMP1=$(uname --kernel-release)
TEMP2=(${TEMP1//[.-]/ })
VERSION=$(((${TEMP2[0]} * 100)\
+ ${TEMP2[1]}))
echo $VERSION
This will still work when the OP's version bumps up to something like 4.1
Upvotes: 0
Reputation: 437052
To complement @devnull's helpful answer with a bash-only solution, using bash's regex-matching operator, =~
:
ver=$([[ $(uname -r) =~ ^([0-9]+)\.([0-9]+) ]];
echo "${BASH_REMATCH[1]}${BASH_REMATCH[2]}")
echo "$ver" # e.g., -> '310', if `uname -r` returned "3.10.34-1-MANJARO"
An alternative solution using bash parameter expansion:
Note: This will only work if the output from uname -r
contains a -
in the 3rd .
-based component - this appears to the case for Linux distributions (but not, for instance, on OSX).
ver=$(uname -r) # get kernel release version, e.g., "3.10.34-1-MANJARO"
ver="${ver%.*-*}" # remove suffix starting with '.' and containing '-'
ver="${ver//.}" # remove periods (a single `/` would do here)
echo "$ver" # e.g., -> '310'
Tip of the hat to @alvits, who points out that uname -r
may have an additional .
component describing the architecture - e.g. 3.8.13-16.2.1.el6uek.x86_64
.
Upvotes: 2
Reputation: 123448
You could use awk
:
awk -F. '{print $1$2}' <<< "3.10.34-1-MANJARO"
or cut
:
cut -d. -f1-2 --output-delimiter='' <<< "3.10.34-1-MANJARO"
Upvotes: 4