Skip to content

Instantly share code, notes, and snippets.

@ljharb
Created February 24, 2019 00:06
Show Gist options
  • Save ljharb/6e70a79ac5fcf11a8b9de654a88642aa to your computer and use it in GitHub Desktop.
Save ljharb/6e70a79ac5fcf11a8b9de654a88642aa to your computer and use it in GitHub Desktop.
nvm_version_greater() {
command awk 'BEGIN {
if (ARGV[1] == "" || ARGV[2] == "") exit(1)
split(ARGV[1], a, /\./);
split(ARGV[2], b, /\./);
for (i=1; i<=3; i++) {
if (a[i] && a[i] !~ /^[0-9]+$/) exit(2);
if (b[i] && b[i] !~ /^[0-9]+$/) { exit(0); }
if (a[i] < b[i]) exit(3);
else if (a[i] > b[i]) exit(0);
}
exit(4)
}' "${1#v}" "${2#v}"
}
set -e
if nvm_version_greater 0.12.0 0.12.87; then echo y; else echo n; fi
@ljharb
Copy link
Author

ljharb commented Feb 24, 2019

bash/sh on my mac exits with 3; zsh and dash properly echo 'n'. Any idea what's going on?

@ljharb
Copy link
Author

ljharb commented Feb 24, 2019

Turns out it's a bug in bash 3.2, fixed in 4.4: bats-core/bats-core#72 (comment) and https://tiswww.case.edu/php/chet/bash/CHANGES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment