Linux kernel version comparision method


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Linux kernel version comparision method
# 1  
Old 07-14-2014
Oracle Linux kernel version comparision method

Hi All,

I am preparing a precheck script for my oracle DB installation.

I am setting an expected version value, actual version can be greater than the expected version or equal to it.

I am using the below logic to test this.

Code:
expected=2.6.32-279.el6.x86_64
received=$(uname -r)

min=$(echo -e $expected"\n"$received|sort -V|head -n 1)
if [ "$min" = "$expected" ];then
       echo "Installed Version is higher than Expected."
       echo "Passed."
else
       echo "== FAILED == Received output is not as expected."
fi


Usually this logic works fine in checking the kernel version.
But recently i came across a server where the kernel version is little different

Code:
uname -r
2.6.32-279.39.1.el6.x86_64

If you look at the output, its has an extra numbers i.e., 39.1.

Can some one help me how to check for such extra characters, because with the current logic it fails to compare when we have extra digits in any one of the values.

Also it would be great if someone can give me a kernel version naming standard for Red Hat Linux.

Regards,
Veer
# 2  
Old 07-14-2014
Can't help with the Red Hat Kernel version naming standard but this might help with this particular issue:

Code:
expected=2.6.32-279.el6.x86_64
received=$(uname -r)

exp_ver=${expected//\.el6.*/}
re_ver=${received//\.el6.*/}

if [ ${#re_ver} -lt ${#received} -a "$re_ver" '>' "$ex_ver" ]
then
    echo "Installed Version is higher than Expected."
    echo "Passed."
else
    echo "== FAILED == Received output is not as expected."
fi

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 07-15-2014
Thanks for your reply Chubler, I have couple of questions related to your code.

Code:
exp_ver=${expected//\.el6.*/}
re_ver=${received//\.el6.*/}

I was not aware of cutting out the string this way, i used to use awk or sed, but this looks more simpler,can you please explain me the syntax?

Code:
if [ ${#re_ver} -lt ${#received} -a "$re_ver" '>' "$ex_ver" ]

In this if statement, you are using '>' symbol for comaparision, why is the comparasion operator in quotes?

Regards,
Veer
# 4  
Old 07-15-2014
Shell pattern substitution

This is the shell pattern substitution (man bash or man ksh should provide details for you). This is much more efficient that using sed or awk as it's internal to the shell and doesn't require loading an external executable before processing.

The substitution I supplied in my solution is a little misleading and could do with some improvement; although it does work for the supplied data.

1. The backslash in \. is unnecessary, the dot character does not need to be escaped - it will only match with it's self, question mark ? is used to represent any single character
2. // is used to replace all occurrences within the string not just the first.
3. .* matches the literal dot followed by zero or more characters. In the example dot does follow el6, but those used to other regular expressions may interpret this as zero or more occurrences of any single character, not dot followed by zero or more characters.

I think a better pattern substitution would be echo ${expected/.el6*/}

String comparisons

Greater than and less than need to be escaped within [ ] context otherwise they are treated as redirection this is not the case with [[ and ]]. It's a matter of choice on using backslash or single quotes. the following are equivalent:

Code:
[ "$a" \< "$b" ]
[ "$a" '<' "$b" ]
[[ "$a" < "$b" ]]

# 5  
Old 07-18-2014
I think i figured out the code to compare kernel & rpm version's on Redhat Linux.
Below is the code that i have written and tested for various RPM's & kernel version.

Code:
expected_ver=2.6.32-279.el6.x86_64
received_ver=2.6.32-279.39.1.el6.x86_64

expected_v=$(echo $expected_ver|awk -F'.' '{if($NF ~ /[xi]*86/ && $(NF-1) ~ /el/) NF=NF-2; else if ($NF ~ /[xi]*86/ && $(NF-1) !~ /el/) NF=NF-1; else if ($NF ~ /el/) NF=NF-1; for (i=1;i<=NF;i++) printf $i".";}'|sed 's/.$//')

received_v=$(echo $received_ver|awk -F'.' '{if($NF ~ /[xi]*86/ && $(NF-1) ~ /el/) NF=NF-2; else if ($NF ~ /[xi]*86/ && $(NF-1) !~ /el/) NF=NF-1; else if ($NF ~ /el/) NF=NF-1; for (i=1;i<=NF;i++) printf $i".";}'|sed 's/.$//')

if [ "$expected_v" = "$received_v" ]; then
	echo "Installed Version is same than Expected."
    echo "Passed."
else
	min=$(echo -e $expected_v"\n"$received_v|sort -V|head -n 1)
	if [ "$min" = "$expected" ];then
       echo "Installed Version is higher than Expected."
       echo "Passed."
	else
       echo "== FAILED == Received output is not as expected."
	fi
fi

# 6  
Old 11-19-2014
I'm only interested in the first 3 'number groups' but this could be expanded as needed:

Code:
	declare -a TEMP2
	TEMP1=$(uname --kernel-release)
	TEMP2=(${TEMP1//[.-]/ })
	VERSION=$(((${TEMP2[0]} * 10000)\
		+ (${TEMP2[1]} * 100)\
		+ ${TEMP2[2]}))

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Regarding firmware and kernel version of HP-UX systems.

Dear All, Please help to find out the patch and firmware versions in HP-UX systems. as uname -r is showing os version and uname -a HP-UX xxxxxx B.11.23 U 9000/800 2781427196 unlimited-user license. Kindly help to find out the existing Kernel and firmware versions. Also I need to... (3 Replies)
Discussion started by: anuragr
3 Replies

2. Red Hat

Kernel version required for installing KVM

Hi friends, I have a created a RHEL 6.4 for installing KVM in Virtual box. My machine is a 64 bit, but virtual box was not allowing to install x86_64 version RHEL. So I had to install 32 bit i686 version of RHEL. The kernel came with the CD was 2.6.32-358.el6.i686. But KVM doesnt support... (2 Replies)
Discussion started by: shyam2j
2 Replies

3. Shell Programming and Scripting

get the perl version (decimal value comparision)

Hi All, can you pls throw some light for below logic -> Check the perl version -> if the version is greater than or equal to 5.8 -> proceed to next step -> else fail Regards Kamal (2 Replies)
Discussion started by: kamauv234
2 Replies

4. Solaris

about the Kernel version ?!!

Hi All I have the below information ... and I want to know how to get the lats Kernel version ? and is this Kernel version SunOS 5.10 Generic_147440-13 last version or what ? - Hostname: server1 Release: 5.10 Kernel architecture: sun4u Application architecture: sparc Hardware... (1 Reply)
Discussion started by: roooooot
1 Replies

5. Red Hat

Downgrade kernel version with yum

Dear all, Now I need to downgrade my current kernel to 2.6.12 I found some pages with Google, and they said that I can do it with yum, but there is not details information. How can I use yum to downgrade kernel version :wall: Please help me Thank you very much (4 Replies)
Discussion started by: Hannibal2010
4 Replies

6. Shell Programming and Scripting

Incorrect kernel version (why).

ps -A | grep firefox | kill `awk '{print $1}'` Warning: /boot/System.map-2.6.35.11-83.fc14.x86_64 has an incorrect kernel version. The command have succeeded but with the warning. The command before the previous one had not been successful. But there was also this warning. (1 Reply)
Discussion started by: Xcislav
1 Replies

7. Debian

how to find out the kernel version of a particular debian release ?

Is there any metrics table available somewhere listing out, which kernel version is there in particular debian release ? Also, can somebody tell which debian release is having the kernel version 2.6.26 ? (1 Reply)
Discussion started by: thegeek
1 Replies

8. Solaris

How can i see kernel patch version?

Hello experts. How can i see kernel patch version? what are advantages applying that patch? Thanx in advance (5 Replies)
Discussion started by: younus_syed
5 Replies

9. Linux

kernel version

Hi all, anyone know what should be the kernel version for red hat linux enterprise 3 update 3 . I have installed kernel version is 2.4.21-4 and some says that it may not be the red hat linux enterprise 3 update 3 . :o thanks Bert (4 Replies)
Discussion started by: bert.n
4 Replies

10. Linux Benchmarks

Dual Xeon 2.6, RedHat EL 4 ES, up to latest release version. 2.6.9 kernel

============================================================== BYTE UNIX Benchmarks (Version 3.11) System -- Linux linux.hhc.ac.uk 2.6.9-11.ELsmp #1 SMP Fri May 20 18:26:27 EDT 2005 i686 i686 i386 GNU/Linux Start Benchmark Run: Wed Jul 6 15:06:07 BST 2005 1 interactive users.... (2 Replies)
Discussion started by: Garp
2 Replies
Login or Register to Ask a Question