Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 19 Dec 2014, 20:58
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to compare versions in a Bash script? (Solved)
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [9 Posts]  
Author Message
linus.cl

Joined: 02 Apr 2014
Posts: 101
Location: Germany

PostPosted: Mon 25 Aug 2014, 14:44    Post subject:  How to compare versions in a Bash script? (Solved)
Subject description: How can I compare two versions in Bash?
 

Hello!

How can I compare two versions in my Bash script?

I have two versions:
Code:

2.7.1-i486s 2.3-2-spup
^new          ^old


They are saved in two variables: $newversion and $oldversion
I try to compare them.

I hope, you can help me... Very Happy

Last edited by linus.cl on Sat 30 Aug 2014, 16:00; edited 1 time in total
Back to top
View user's profile Send private message 
6502coder

Joined: 23 Mar 2009
Posts: 113
Location: Western United States

PostPosted: Mon 25 Aug 2014, 15:53    Post subject:  

You mean that you just want to test two shell variables to see if they are the same string? That's easy:

Code:

if [ $newversion  =  $oldversion ]
then
   echo same
else
   echo different
fi
Back to top
View user's profile Send private message 
linus.cl

Joined: 02 Apr 2014
Posts: 101
Location: Germany

PostPosted: Mon 25 Aug 2014, 15:57    Post subject:  

No, i'd like to check if $newversion is newer.

I found an application: vercmp.
https://www.archlinux.org/pacman/vercmp.8.html

But I can't use it. (I don't understand it)
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: Mon 25 Aug 2014, 18:29    Post subject:  

Code:
if vercmp ${NEWVERSION} gt ${OLDVERSION} ; then
   echo $NEWVERSION
fi

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 506
Location: Japan

PostPosted: Mon 25 Aug 2014, 22:27    Post subject:  

linus.cl wrote:
I found an application: vercmp.
No need for an application. Try this:
Code:
newversion="2.7.1-i486s"
oldversion="2.3-2-spup"
[[ "$newversion" > "$oldversion" ]] && echo "newversion is newer" || echo "oldversion is newer"

The > operator compares 2 strings by their alphabetic ASCII order. Requires a double bracket test construct to work.
Back to top
View user's profile Send private message 
gcmartin


Joined: 14 Oct 2005
Posts: 4506
Location: Earth

PostPosted: Tue 26 Aug 2014, 10:20    Post subject:  

Hello @Linus.cl

Noticing some of the very good things you are aiming into the community, you may find this tool to have "tremendous " advantage to your use and development efforts. Install and observe. It will meet much much more than you ask while accelerating your work efforts. And, there is no system penalty in its presence as a tool.

Here to help

_________________
Get ACTIVE Create Circles; Do those good things which benefit people's needs!
We are all related ... Its time to show that we know this!
3 Different Puppy Search Engine or use DogPile
Back to top
View user's profile Send private message 
jamesbond

Joined: 26 Feb 2007
Posts: 2232
Location: The Blue Marble

PostPosted: Tue 26 Aug 2014, 10:39    Post subject:  

There are a few versions of "vercmp" floating around. Since you're building for Puppy, might as well use Puppy's own vercmp. It's here: https://github.com/puppylinux-woof-CE/woof-CE/blob/master/woof-code/boot/vercmp.c. If you google "barry kauler vercmp" you will find a few links.
_________________
Fatdog64, Slacko and Puppeee user. Puppy user since 2.13.
Contributed Fatdog64 packages thread
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4424

PostPosted: Wed 27 Aug 2014, 22:16    Post subject:  

sort -V or sort --version-sort works too
use head -n1 or tail -n1 for the oldest/newest (works for comparing 2 or more)

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
Karl Godt


Joined: 20 Jun 2010
Posts: 3982
Location: Kiel,Germany

PostPosted: Mon 15 Dec 2014, 14:11    Post subject:  

I would use case for that

Code:
case $BASH_VERSION in
1.*) : do_something;;
2.[0-2].*) : do_something_else;;
2.3.*) echo OLD VERSION;;
2.[4-6].*) : do_something_else_more;;
2.7.*) echo NEW VERSION;;
*) echo UNHANDLED bash version $BASH_VERSION;;
esac


5.2 Bash Variables

These variables are set or used by Bash, but other shells do not normally treat them specially.

BASH_VERSINFO

A readonly array variable (see Arrays) whose members hold version information for this instance of Bash. The values assigned to the array members are as follows:

BASH_VERSINFO[0]

The major version number (the release).
BASH_VERSINFO[1]

The minor version number (the version).
BASH_VERSINFO[2]

The patch level.
BASH_VERSINFO[3]

The build version.
BASH_VERSINFO[4]

The release status (e.g., beta1).
BASH_VERSINFO[5]

The value of MACHTYPE.

BASH_VERSION

The version number of the current instance of Bash.

http://www.gnu.org/software/bash/manual/bashref.html#Invoking-Bash


Bash version 2.Y may not know some of these variables .

_________________
«Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal Razz
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 1 of 1 [9 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
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
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0678s ][ Queries: 12 (0.0057s) ][ GZIP on ]