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.