Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have to update a script compilation from time to time but I haven't any idea how to start a version check script in bash.

I've got a version file with 3 vars: version=0, subversion=0 and subsubversion=1 and I've got a web server that serves the updates with install routines as bash scripts. In a directory called updates:

/updates/
0.0.1
0.0.2
0.0.3

and a directory called current where the current version is stored.

/current/
0.0.1
version

Now I'd like to have a script that takes $version, $subversion and $subsubversion and determines witch update to download and execute. After this part has finished the update script should increment a var so that it downloads the next update if it is available. Let's call it nextversion.

I stuck with incrementing the three vars and thenextversion variable in the update script.

What could I do to increment the tree vars and a fourth var to the next higher level. How do I handle it if I go from 0.0.9 to 0.1.0 or even from 0.9.9 to 1.0.0 and so on. Without producing a lot of if-else-elif statements. I'm sadly not good in figuring out loops.

share|improve this question
1  
Version numbers are just numbers. So after 0.9.0 you of course can also have a 0.10.0. But if you want to make things easy, just use something like a revision number which you simply count up. So just stick to 1, 2, 3, 4, and so on. –  Shi Aug 10 '13 at 20:15
    
This sounds like a really inefficient way to do the updating -- if a particular script has been updated 10 times between the version on the client and the latest one, should it really download each of the 9 intermediate versions in sequence? –  Gordon Davisson Aug 12 '13 at 3:47
    
Basically it should be able to update the system regardless witch version is installed. I haven't any idea how to use git for this. If I had I would use it instead of bash. –  Josh Aug 13 '13 at 6:47
add comment

1 Answer

This breaks after 9.9.9

#!/bin/bash

version=0
subversion=0
subsubversion=1

updateVersion()
{
  if [ "$subsubversion" -lt 9 ]; then
    subsubversion=$((subsubversion+1));
  elif [ "$subsubversion" = 9 ] && [ "$subversion" -lt 9 ]; then
    subsubversion="0"; subversion=$((subversion+1));
  elif [ "$subsubversion" = 9 ] && [ "$subversion" = 9 ] && [ "$version" -lt 9 ]; then
    subsubversion="0"; subversion="0"; version=$((version+1));
  else
    echo "overflow"
  fi
}

printVersion()
{
  echo "${version}.${subversion}.${subsubversion}";
}

updateVersion
printVersion

Perhaps you'd rather use git instead?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.