Skip to content

Instantly share code, notes, and snippets.

@zeroboo
Last active November 17, 2022 07:37
Show Gist options
  • Save zeroboo/4d30a66ac2d775f835da9bb0c56092d8 to your computer and use it in GitHub Desktop.
Save zeroboo/4d30a66ac2d775f835da9bb0c56092d8 to your computer and use it in GitHub Desktop.
Increase minor of a semantic version in a text file.
#!/bin/bash
#Increase version in a file
version_file="$1"
major=0
minor=0
build=0
version=$(<$version_file)
# replace \n with space
options="${version//$'\n'/ }"
# break down the version number into it's components
regex="([0-9a-zA-Z]+).([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: ./increase_version_file.sh version_number [major/feature/bug]"
exit -1
fi
# echo the new version number
echo "${major}.${minor}.${build}">$version_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment