0

I use the following bash/shell script to semi-automate the git add/commit/push routine on my project:

git_push.sh

#!/bin/bash

# Mini Config
RColor='\e[0m'
Red='\e[0;31m';
Green='\e[0;32m';
Yellow='\e[0;33m'

# Change To Working Directory
clear;
cd $HOME/public_html/iwms_reboot;

# Get Git Commit Notes
echo -en "\r\n${Green}Enter commit notes: ${Yellow}";
read notes;
if [[ -z "$notes" ]]
then
    echo -e "\r\n${Red}ERROR: You have not entered any git commit notes.${RColor}\r\n";
    exit 0;
fi
echo -e "${RColor}";

# Git Add, Commit & Push
git add .;
git commit -m "${notes}";
echo -e "\r\n";
git push;
echo -e "\r\n";

This works perfectly fine.

I want to take this one step further. On my prject, there is a single file called version.php with the following lines of code:

<?php

// Script Version
$script_version = 'v1.0.5';

?>

My question is, is it possible to use bash/shell scripting to load this file's content and find the number after the 2nd period (.) and increment it by one?

i.e. v1.0.5 will become v1.0.6

This way, I can run this version number updating function before my (git add/commit/push) routine to implement an automatic minor version number update functionality on my project. I.e. script version number goes up automatically every time I commit.

3
  • 1
    Check stackoverflow.com/questions/8653126/… Commented Dec 1, 2014 at 13:27
  • I don't see what the script at the top of your question has to do with the question itself. Perhaps you should remove it. Have you tried doing anything to increment the number? If so, that would be much more useful to include. Commented Dec 1, 2014 at 13:53
  • I found my answer from stackoverflow.com/questions/8653126/…. I am going post it soom. Commented Dec 1, 2014 at 13:54

2 Answers 2

1

If you want a 'pure-bash' solution, here it is...:

#!/bin/bash

new_version=''
increment_version_number () {
  declare -a part=( ${1//\./ } )
  declare    new
  declare -i carry=1

  for (( CNTR=${#part[@]}-1; CNTR>=0; CNTR-=1 )); do
    len=${#part[CNTR]}
    new=$((part[CNTR]+carry))
    [ ${#new} -gt $len ] && carry=1 || carry=0
    [ $CNTR -gt 0 ] && part[CNTR]=${new: -len} || part[CNTR]=${new}
  done
  new="${part[*]}"
  new_version="${new// /.}";
}

version=$(sed version.php -e "s/\$script_version = 'v//" | sed -e "s/';$//")
increment_version_number $version

echo $new_version;

UPDATE: Code for a two digits version numbers (as requested in comment):

#!/bin/bash

new_version=''
increment_version_number () {
  declare -a part=( ${1//\./ } )
  declare    new
  declare -i carry=1

  for (( CNTR=${#part[@]}-1; CNTR>=0; CNTR-=1 )); do
    len=${#part[CNTR]}
    new=$((part[CNTR]+carry))
    [ ${#new} -gt $(($len+1)) ] && carry=1 || carry=0
    part[CNTR]=${new}
  done
  new="${part[*]}"
  new_version="${new// /.}";
}

version=$(grep "\$script_version" version.php | sed -e "s/\$script_version = 'v//" | sed -e "s/';$//")
increment_version_number $version

echo $new_version;

(warning: not fully tested code...)

Sign up to request clarification or add additional context in comments.

3 Comments

Damn that looks nice too. +1
This version makes 1.0.9 into 1.1.0 - Is it possible to make the number go upto 99 rather than making 9 as max?
For example 1.0.9 should become 1.0.10 and 1.0.99 becomes 1.1.0 etc...
0

Thanks to fredtantini and using the answer from How to increment version number in a shell script?, I have come up with the following solution to my original problem.

I first created a file called version.data and put the text 1.0.5 in it.

Then I updated my PHP script like so:

<?php

// Script Version
$script_version = 'v'. trim(file_get_contents(app_path() .'/version.data'));

?>

Then I have created the following gawk script called version_updater.sh (next to my git_push.sh script):

#!/usr/bin/gawk -f

BEGIN {
    printf("%s", inc(ARGV[1]))
}

function inc(s, a, len1, len2, len3, head, tail)
{
    split(s, a, ".")

    len1 = length(a)
    if(len1==0)
        return -1
    else if(len1==1)
        return s+1

    len2 = length(a[len1])
    len3 = length(a[len1]+1)

    head = join(a, 1, len1-1)
    tail = sprintf("%0*d", len2, (a[len1]+1)%(10^len2))

    if(len2==len3)
        return head "." tail
    else
        return inc(head) "." tail
}

function join(a, x, y,    s)
{
    for(i=x; i<y; i++)
        s = s a[i] "."
    return s a[y]
}

Then I have updated my git_push.sh script like so:

#!/bin/bash

# Mini Config
RColor='\e[0m'
Red='\e[0;31m';
Green='\e[0;32m';
Yellow='\e[0;33m';
Source=$HOME/public_html/iwms_reboot;

# Increment Script Version
CurrentVersion=`cat "$Source/app/version.data"`;
NewVersion=`./version_updater.sh $CurrentVersion`;

# Change To Working Directory
clear;
cd $Source;

# Get Git Commit Notes
echo -en "\r\n${Green}Enter commit notes: ${Yellow}";
read notes;
if [[ -z "$notes" ]]
then
    echo -e "\r\n${Red}ERROR: You have not entered any git commit notes.${RColor}\r\n";
    exit 0;
fi
echo -e "${RColor}";

# Update Script Version
echo $NewVersion > $Source/app/version.data;

# Git Add, Commit & Push
git add .;
git commit -m "${notes}";
echo -e "\r\n";
git push;
echo -e "\r\n";

... and that's it, it works.

when the script runs, it reads the current version, passes it to the version_updater.sh to programatically increment it and put the return value into a variable.

Just before I commit, I update my version.data file with the updated version number. Now when I commit, I commit with the new version number.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.