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 24 Oct 2014, 09:37
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 Posts_count  
Author Message
linus.cl

Joined: 02 Apr 2014
Posts: 88
Location: Germany

PostPosted: Mon 25 Aug 2014, 14:44    Post_subject:  How to compare versions in a Bash script? (Solved)
Sub_title: 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

Edited_time_total
Back to top
View user's profile Send_private_message 
6502coder

Joined: 23 Mar 2009
Posts: 96
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: 88
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: 433
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: 4368
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: 2230
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: 4353

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 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


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