I need the latest version out of these 4 kernel versions.
- 4.18.0-187.el8.x86_64
- 4.18.0-193.14.3.el8_2.x86_64
- 4.18.0-193.el8.x86_64
- 4.18.0-80.el8.x86_64
I had initially used numeric sort (which returns the 0-80 version incorrectly) before moving onto version sort for the same
latest_kernel_in_use=$(ls boot/vmlinuz* | sed 's/\/boot\/vmlinuz-//' | sort -V | tail -n1 )
The command still returns 4.18.0-193.el8.x86_64 versus the desired 4.18.0-193.14.3.el8_2.x86_64 output.
Help me out with the correction in the command. I tested additionally and it's really the suffixes .el8.x86_64 complicating the sorting.
[Edit] So I did it eventually, I removed the trailing alphabet/alphanumeric sequence, identifier being there is atleast one alphabet involved whether RPM kernel version is 4.18.0-193.14.3.el8_2.x86_64 or something like 4.18.0-193.14.3-generic as seen in Ubuntu
latest_kernel_in_use=$(ls boot/vmlinuz* | sed 's/boot\/vmlinuz-//' | sed 's/[.-][[:alpha:]][[:alnum:][:punct:]]*//' | sort -V | tail -n1)
The Output with this is 4.18.0-193.14.3
I can work from there.
CodePudding user response:
seems to be because "e" > 1
I came up with this horror but I am sure there is a better way
#!/bin/bash
# list your kernels and remove "vmlinuz-" prefix
KERNELS=$(find boot/ -iname "vmlinuz*" -exec basename {} \; | sed s/vmlinuz-//g)
# declare empty arrays (this will be arrays to contain our version and suffix strings)
VERSIONS_ARRAY=()
SUFFIXES_ARRAY=()
# we loop thought each kernel version
for KERNEL in $KERNELS;do
# we will split each part of the kernel version on the dot (".")
IFS="."
read -ra KERNEL_VERSION <<< "$(echo $KERNEL)"
# we define 2 empty strings to collect the kernel parts
VERSION_STRING=""
SUFFIX_STRING=""
# we set back field separator to space
IFS=" "
# we loop throug all parts of kernel version
for KERNEL_PART in ${KERNEL_VERSION[@]};do
#echo -n $KERNEL_PART
# if we encounter el8 or el8_2 or x86_64 string we append to the SUFFIX_STRING
if [[ "$KERNEL_PART" == "el8" ]] || [[ "$KERNEL_PART" == "el8_2" ]] || [[ "$KERNEL_PART" == "x86_64" ]] ;then
SUFFIX_STRING="${SUFFIX_STRING}${KERNEL_PART}."
# else, we append to the VERSION_STRING
else
VERSION_STRING="${VERSION_STRING}${KERNEL_PART}."
fi
done
# we append the VERSION_STRING in VERSION_STRINGS
VERSIONS_ARRAY =("$VERSION_STRING")
# we append the SUFFIX_STRING in SUFFIXES_ARRAYS
SUFFIXES_ARRAY =("$SUFFIX_STRING")
done
# we set the field separator to split on linebreaks
IFS=$'\n'
# we find the highest value in version array
HIGHEST_VERSION=$(echo "${VERSIONS_ARRAY[*]}" | sort -V | tail -1)
# we get the position of highest value in array
for INDEX in ${VERSIONS_ARRAY[@]};do
if [[ "$VERSIONS_ARRAY{[$INDEX]}" = "$HIGHEST_VERSION" ]]; then
INDEX_OF_HIGHER_VERSION=$INDEX
fi
done
# we get the corresponding suffix by index
HIGHEST_VERSION_SUFFIX=${SUFFIXES_ARRAY[$INDEX_OF_HIGHER_VERSION]}
# we remove the trailing dot in suffix
len=${#HIGHEST_VERSION_SUFFIX}
HIGHEST_VERSION_SUFFIX=${HIGHEST_VERSION_SUFFIX::len-1}
echo "vmlinuz-${HIGHEST_VERSION}${HIGHEST_VERSION_SUFFIX}"
# we set back field separator to space
IFS=" "
CodePudding user response:
So I did it eventually, I removed the trailing alphabet/alphanumeric sequence, identifier being there is atleast one alphabet involved whether RPM kernel version is 4.18.0-193.14.3.el8_2.x86_64 or something like 4.18.0-193.14.3-generic as seen in Ubuntu
latest_kernel_in_use=$(ls boot/vmlinuz* | sed 's/boot\/vmlinuz-//' | sed 's/[.-][[:alpha:]][[:alnum:][:punct:]]*//' | sort -V | tail -n1)
The Output with this is 4.18.0-193.14.3
I can work from here.