Skip to content

Instantly share code, notes, and snippets.

@webmaster128
Created October 30, 2014 21:08
Show Gist options
  • Save webmaster128/9e52e1cec044883fee80 to your computer and use it in GitHub Desktop.
Save webmaster128/9e52e1cec044883fee80 to your computer and use it in GitHub Desktop.
Bash version compare operator test
#!/bin/bash
echo "Test 1: '<'"
if [[ 13.10 < 14.04 ]]; then echo "ok"; else echo "fail"; fi;
if [[ 13.10 < 14.04.1 ]]; then echo "ok"; else echo "fail"; fi;
echo
echo "Test 2: '-lt'"
if [[ 13.10 -lt 14.04 ]]; then echo "ok"; else echo "fail"; fi;
if [[ 13.10 -lt 14.04.1 ]]; then echo "ok"; else echo "fail"; fi;
echo
echo "Test 3: '-le'"
if [[ 13.10 -le 14.04 ]]; then echo "ok"; else echo "fail"; fi;
if [[ 13.10 -le 14.04.1 ]]; then echo "ok"; else echo "fail"; fi;
echo
echo "Test 4: '=='"
if [[ 13.10 == 13.10 ]]; then echo "ok"; else echo "fail"; fi;
if [[ 14.04.1 == 14.04.1 ]]; then echo "ok"; else echo "fail"; fi;
if [[ 13.10 == 14.04 ]]; then echo "fail"; else echo "ok"; fi;
if [[ 13.10 == 14.04.1 ]]; then echo "fail"; else echo "ok"; fi;
if [[ 14.04 == 14.04.1 ]]; then echo "fail"; else echo "ok"; fi;
echo
echo "Test 5: '>'"
if [[ 13.10 > 14.04 ]]; then echo "fail"; else echo "ok"; fi;
if [[ 13.10 > 14.04.1 ]]; then echo "fail"; else echo "ok"; fi;
if [[ 14.04 > 13.10 ]]; then echo "ok"; else echo "fail"; fi;
if [[ 14.04.1 > 13.10 ]]; then echo "ok"; else echo "fail"; fi;
echo
echo "Test 6: '-gt'"
if [[ 13.10 -gt 14.04 ]]; then echo "fail"; else echo "ok"; fi;
if [[ 13.10 -gt 14.04.1 ]]; then echo "fail"; else echo "ok"; fi;
if [[ 14.04 -gt 13.10 ]]; then echo "ok"; else echo "fail"; fi;
if [[ 14.04.1 -gt 13.10 ]]; then echo "ok"; else echo "fail"; fi;
echo
echo "Test 7: '-ge'"
if [[ 13.10 -ge 14.04 ]]; then echo "fail"; else echo "ok"; fi;
if [[ 13.10 -ge 14.04.1 ]]; then echo "fail"; else echo "ok"; fi;
if [[ 14.04 -ge 13.10 ]]; then echo "ok"; else echo "fail"; fi;
if [[ 14.04.1 -ge 13.10 ]]; then echo "ok"; else echo "fail"; fi;
echo
echo "Test 8: '>='"
if [[ 13.10 >= 14.04 ]]; then echo "fail"; else echo "ok"; fi;
if [[ 13.10 >= 14.04.1 ]]; then echo "fail"; else echo "ok"; fi;
if [[ 14.04 >= 13.10 ]]; then echo "ok"; else echo "fail"; fi;
if [[ 14.04.1 >= 13.10 ]]; then echo "ok"; else echo "fail"; fi;
echo
# Test 1: '<'
# ok
# ok
#
# Test 2: '-lt'
# ./test.sh: line 9: [[: 13.10: syntax error: invalid arithmetic operator (error token is ".10")
# fail
# ./test.sh: line 10: [[: 13.10: syntax error: invalid arithmetic operator (error token is ".10")
# fail
#
# Test 3: '-le'
# ./test.sh: line 14: [[: 13.10: syntax error: invalid arithmetic operator (error token is ".10")
# fail
# ./test.sh: line 15: [[: 13.10: syntax error: invalid arithmetic operator (error token is ".10")
# fail
#
# Test 4: '=='
# ok
# ok
# ok
# ok
# ok
#
# Test 5: '>'
# ok
# ok
# ok
# ok
#
# Test 6: '-gt'
# ./test.sh: line 34: [[: 13.10: syntax error: invalid arithmetic operator (error token is ".10")
# ok
# ./test.sh: line 35: [[: 13.10: syntax error: invalid arithmetic operator (error token is ".10")
# ok
# ./test.sh: line 36: [[: 14.04: syntax error: invalid arithmetic operator (error token is ".04")
# fail
# ./test.sh: line 37: [[: 14.04.1: syntax error: invalid arithmetic operator (error token is ".04.1")
# fail
#
# Test 7: '-ge'
# ./test.sh: line 41: [[: 13.10: syntax error: invalid arithmetic operator (error token is ".10")
# ok
# ./test.sh: line 42: [[: 13.10: syntax error: invalid arithmetic operator (error token is ".10")
# ok
# ./test.sh: line 43: [[: 14.04: syntax error: invalid arithmetic operator (error token is ".04")
# fail
# ./test.sh: line 44: [[: 14.04.1: syntax error: invalid arithmetic operator (error token is ".04.1")
# fail
#
# Test 8: '>='
# ./test.sh: line 48: syntax error in conditional expression
# ./test.sh: line 48: syntax error near `14.04'
# ./test.sh: line 48: `if [[ 13.10 >= 14.04 ]]; then echo "fail"; else echo "ok"; fi;'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment