Skip to content

Instantly share code, notes, and snippets.

@mietzen
Forked from siddharthkrish/version.sh
Last active August 16, 2023 04:59
Show Gist options
  • Save mietzen/cd33efb65f91619201a45ac50dcfc29e to your computer and use it in GitHub Desktop.
Save mietzen/cd33efb65f91619201a45ac50dcfc29e to your computer and use it in GitHub Desktop.
simple bash script to increment the version number of the format major.minor.bug
#!/usr/bin/env bash
help () {
echo "usage: ./bump-version version_number [major/minor/bug]"
}
# Check input remove prepending 'v'
if grep -q -c -E '^v?[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}$' <<< ${1}; then
version=$(sed 's/^.\{1\}//g' <<< ${1})
else
help
fi
major=0
minor=0
bug=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]}"
bug="${BASH_REMATCH[3]}"
fi
# check paramater to see which number to increment
if [[ "$2" == "major" ]]; then
major=$(echo $major+1 | bc)
minor=0
bug=0
elif [[ "$2" == "minor" ]]; then
minor=$(echo $minor + 1 | bc)
bug=0
elif [[ "$2" == "bug" ]]; then
bug=$(echo $bug + 1 | bc)
else
help
exit 1
fi
# echo the new version number
echo "${major}.${minor}.${bug}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment