Bash-compare-versions

From I Will Fear No Evil
Revision as of 08:58, 22 October 2021 by Chubbard (talk | contribs) (Created page with "This is a simplistic way to compare version numbers. This is useful for when commands change over time, and you do not know which or what kind of host will have which version...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This is a simplistic way to compare version numbers. This is useful for when commands change over time, and you do not know which or what kind of host will have which version of something.

echo -e "2.22\n11.3.4" | sort -V
2.22
11.3.4
printf '%s\n2.15\n' $(ldd --version | sed -n '1s/.* //p') | sort -V | head -n 1
printf '%s\n2.22\n' $(git --version | sed -n '1s/.* //p') | sort -V

Example:

if [[ $(printf '%s\n2.22\n' $(git --version | sed -n '1s/.* //p') | sort -V) != "2.22" ]]; then
  echo "Earlier version than cutoff"
  someCommand
else
  echo "Later version of cutoff"
  someCommand_with_different_args
fi