Categories
Uncategorized

Bash: Getting the systemd version and comparing it

This is just a quick post, but I wanted to spare both future me, and anyone else, from struggling with the precise syntax to extract the systemd version in a bash script and applying some work-around on older versions.

I recently had to work around a bug in systemd (specifically https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=927911), and needed to modify a service file if the systemd version was less than 241.

To do this, you can extract the version using systemctl --version, pipe it to a fun sed pattern, then use it:

#!/bin/bash

systemdver=$(systemctl --version | sed -nE "s/systemd ([0-9]+).*/\1/p")

# If the systemd version is prior to 241, do something
if [[ $systemdver -lt 241 ]];
then
   echo 'do whatever'
fi

Replace 241 with which ever version you need to compare against.

That’s it, hope this helps someone.

Leave a Reply

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s