Bash-compare-versions: Difference between revisions

From I Will Fear No Evil
Jump to navigation Jump to search
(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...")
 
mNo edit summary
Line 16: Line 16:
</pre>
</pre>


Example:
Example: (show the git branch you are on)
<pre>
<pre>
if [[ $(printf '%s\n2.22\n' $(git --version | sed -n '1s/.* //p') | sort -V) != "2.22" ]]; then
if [[ $(printf '%s\n2.22\n' $(git --version | sed -n '1s/.* //p') | sort -V) != "2.22" ]]; then
   echo "Earlier version than cutoff"
   echo "Earlier version than cutoff"
   someCommand
   git rev-parse --abbrev-ref HEAD
else
else
   echo "Later version of cutoff"
   echo "Later version of cutoff"
   someCommand_with_different_args
   git branch --show-current
fi
fi
</pre>
</pre>
[[Category:bash]]
[[Category:bash]]

Revision as of 08:59, 22 October 2021

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: (show the git branch you are on)

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