bash script to increment WordPress Theme Version

A BASH script I assembled to automatically update the version number of a WordPress theme’s style.css file and commit the changes to git. I’m a bit of newbie when it comes to command line git so use with caution.

#!/bin/bash

# Update version number and commit WordPress theme
# Uses https://gist.github.com/siddharthkrish/32072e6f97d7743b1a7c47d76d2cb06c

fname="style.css"
version=`grep 'Version' ${fname}`
major=0
minor=0
build=0

# break down the version number into it's components
regex="(Version: )([0-9]+).([0-9]+).([0-9]+)"
if [[ $version =~ $regex ]]; then
  major="${BASH_REMATCH[2]}"
  minor="${BASH_REMATCH[3]}"
  build="${BASH_REMATCH[4]}"
fi

old_version="${major}.${minor}.${build}"
# check paramater to see which number to increment
if [[ "$1" == "minor" ]]; then
  minor=$(echo $minor + 1 | bc)
elif [[ "$1" == "build" ]]; then
  build=$(echo $build + 1 | bc)
elif [[ "$1" == "major" ]]; then
  major=$(echo $major+1 | bc)
else
  echo "usage: ./commit.sh [major/minor/build]"
  exit -1
fi
new_version="${major}.${minor}.${build}"
# echo the new version number
echo "Old version: ${old_version}"
echo "New version: ${new_version}"
sed -i "s/${old_version}/${new_version}/" ${fname}

# git
git tag $new_version
git add -A
git commit --all
git push origin $new_version
git push
echo "Completed"

allrite

Irreverently irrelevant. Sysadmin, developer, web dude in a science research agency. WordPress, Japan, planes, trains, Arduino, Raspberry Pi/Pico, puns, dad jokes, etc

Tags:

Submit a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s