Skip to content

Instantly share code, notes, and snippets.

@siddharthkrish
Created July 3, 2017 15:14
Show Gist options
  • Save siddharthkrish/32072e6f97d7743b1a7c47d76d2cb06c to your computer and use it in GitHub Desktop.
Save siddharthkrish/32072e6f97d7743b1a7c47d76d2cb06c to your computer and use it in GitHub Desktop.
simple bash script to increment the version number of the format major.minor.build
#!/bin/bash
version="$1"
major=0
minor=0
build=0
# break down the version number into it's components
regex="([0-9]+).([0-9]+).([0-9]+)"
if [[ $version =~ $regex ]]; then
major="${BASH_REMATCH[1]}"
minor="${BASH_REMATCH[2]}"
build="${BASH_REMATCH[3]}"
fi
# check paramater to see which number to increment
if [[ "$2" == "feature" ]]; then
minor=$(echo $minor + 1 | bc)
elif [[ "$2" == "bug" ]]; then
build=$(echo $build + 1 | bc)
elif [[ "$2" == "major" ]]; then
major=$(echo $major+1 | bc)
else
echo "usage: ./version.sh version_number [major/feature/bug]"
exit -1
fi
# echo the new version number
echo "new version: ${major}.${minor}.${build}"
@Derioss
Copy link

Derioss commented Nov 12, 2020

Thks.
You save me time !
i have adapted your's script for my needs https://github.com/Derioss/increment_versions.sh

@sweisser
Copy link

I love it!
Simple and understandable.

@rgpatil93
Copy link

I have copied exact code to run in my script inside a jenkins job. I have tested it on my local machine it worked but I have spent lot of time on thia and still get working on jenkins server. I get [[: not found

@rgpatil93
Copy link

Uploading IMG_20210719_205013.jpg…

@rgpatil93
Copy link

Request for quick help

@Derioss
Copy link

Derioss commented Jul 19, 2021

Request for quick help

This code is for bash.

[[ only bash and same for this synthax "${BASH_REMATCH[1]} (Shell Parameter Expansion).

I think your's jenkins server use sh.

type bash on your's jenkins execution env, if exist, use bash version.sh

(or use maven release :) )

@rgpatil93
Copy link

I missed to mention where the error was. It was if condition 【【

@Lxrdknows77
Copy link

Okay., but when major or minor version increment, next point should be sweep to zero. Like 1.2.4 minor ---- > 1.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment