Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[bash] Rechnen mit Versionsnummern?
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Mon Nov 11, 2013 6:15 pm    Post subject: [bash] Rechnen mit Versionsnummern? Reply with quote

Hallo Zusammen,

Ich habe da ein kleines Problem und stehe da wohl wieder mal auf dem Schlauch... :?

Ich möchte in einem Shellscript eine Bedingung einbauen, die sich z.B. auf die Kernelversion bezieht, in etwa so:

Code:
if [ $KERNELVERSION -le 3.9.9 ] ; then
 foo
.....


Allerdings habe ich keine Idee, wie ich das mit den Subversionen regeln kann?

Mein erster Gedanke war, die Punkte zu ignorieren, aber dann wäre z.B. ja 3.8.22 neuer (größer) als 3.12.0.

Hat Jemand eine Idee, wie man das lösen könnte?
Back to top
View user's profile Send private message
franzf
Advocate
Advocate


Joined: 29 Mar 2005
Posts: 4565

PostPosted: Mon Nov 11, 2013 7:42 pm    Post subject: Reply with quote

An den Punkten zerlegen und dann major, minor, bugfix-Version vergleichen.
Back to top
View user's profile Send private message
l3u
Advocate
Advocate


Joined: 26 Jan 2005
Posts: 2537
Location: Konradsreuth (Germany)

PostPosted: Thu Nov 14, 2013 5:46 pm    Post subject: Reply with quote

Ich würd das so machen:
Code:
#!/bin/bash

version="3.10.17"
condVersion="3.11.17"

versionArray=($(echo "$version" | tr "." " "))
condVersionArray=($(echo "$condVersion" | tr "." " "))

state="eq"

for i in $(seq ${#condVersionArray[@]}); do
   
   i=$((i - 1))

   if [[ ${versionArray[i]} -lt ${condVersionArray[i]} ]]; then
      state="lt"
      break
   fi

   if [[ ${versionArray[i]} -gt ${condVersionArray[i]} ]]; then
      state="gt"
      break
   fi

done

echo "$version $state $condVersion"

Halt in ne Funktion packen und dann das Ergebnis auswerten in der eigentliche if-Anweisung. Keine Ahnung, ob das mit einer anderen Shell als der Bash geht, aber Bash kann’s … berücksichtigt natürlich im Moment keine "-r1" hinten dran. Müsst man halt entsprechend noch verarbeiten.
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Thu Nov 14, 2013 7:16 pm    Post subject: Reply with quote

An diesen Thread habe schon gar nicht mehr gedacht. :lol:


Ich habe es so gelöst: ;)


Code:
MIN_VERSION=31200

A=$(uname -r)
RELEASE=${A%%\.*}
A=${A#*\.}
MAJOR=${A%%\.*}
A=${A#*\.}
MINOR=${A%%\-*}

if [ $((RELEASE*10000 + MAJOR*100 + MINOR)) -le $MIN_VERSION ] ; then
 MYPARMS="1"
else
 MYPARMS="0"
fi
Back to top
View user's profile Send private message
l3u
Advocate
Advocate


Joined: 26 Jan 2005
Posts: 2537
Location: Konradsreuth (Germany)

PostPosted: Thu Nov 14, 2013 11:24 pm    Post subject: Reply with quote

Oder so.
Back to top
View user's profile Send private message
sirro
Veteran
Veteran


Joined: 20 Jul 2003
Posts: 1472
Location: aachen.nrw.de.eu

PostPosted: Sat Nov 16, 2013 11:34 am    Post subject: Reply with quote

Weitere Moeglichkeiten:

http://stackoverflow.com/questions/4023830/bash-how-compare-two-strings-in-version-format

Da gibt es eine laengliche Funktion, die vieles beruecksichtigt und eine die sich recht simpel auf "sort -V" verlaesst.

Code:
verlte() {
    [  "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Sun Nov 17, 2013 7:04 am    Post subject: Reply with quote

Solange die Kernel-Version der Gentoo-Konvention folgt (also wenn Du keine künstlichen Anhänge wie -hardened hast), kannst Du auch versionsort von eix benutzen, das keine Einschränkung an maximal zweistellige Minor-Versionen hat
Code:
VERSION=1.1.3000
case `versionsort 1.13.0 "x-$VERSION"` in 1.13.0'
'*) echo "$VERSION ist älter als 1.13.0";;
*) echo "$VERSION ist jünger als 1.13.0";;
esac
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum