Thursday, March 31, 2022

[SOLVED] Version sorting RPM Kernel string with numbers in bash returns incorrect result

Issue

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.


Solution

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.



Answered By - thedarksage
Answer Checked By - Senaida (WPSolving Volunteer)