Correct mistakes

This commit is contained in:
Tiago Programmer 2019-12-20 18:47:45 +00:00 committed by GitHub
parent 3482835fcc
commit 1a2f039bf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,54 +3,58 @@
# Note: using bash for more power in testing (the final user doesn't need it). # Note: using bash for more power in testing (the final user doesn't need it).
# Global Variables # Global Variables
G_NANO_VERSION="4.6.0" readonly G_NANO_VERSION="4.6.0"
G_SHELLCHECK_VERSION="0.6.0" readonly G_SHELLCHECK_VERSION="0.6.0"
# Functions # Functions
# Compare Version # Compare Version
# Compare the first version (x.x.x format) against second one. # Compare the first version (x.x.x format) against second one.
# Returns true if $1 => $2, false otherwise. # Returns 0 if $1 => $2, 1 otherwise.
# In error returns 2.
# Sources: # Sources:
# https://unix.stackexchange.com/questions/285924/how-to-compare-a-programs-version-in-a-shell-script # https://unix.stackexchange.com/questions/285924/how-to-compare-a-programs-version-in-a-shell-script
# https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash # https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
# Test table: # Test table:
# req | get | test | res # req | get | test | res
# ------+-------+-------+------- # -------+-------+-------+-----------
# 2.0.0 | 1.0.0 | 1.0.0 | true # 2.0.0 | 1.0.0 | 1.0.0 | true = 0
# 1.0.0 | 2.0.0 | 1.0.0 | false # 1.0.0 | 2.0.0 | 1.0.0 | false = 1
# 0.5.3 | 0.5.3 | 0.5.3 | true # 0.5.3 | 0.5.3 | 0.5.3 | true = 0
f_compare_version(){ f_compare_version(){
local required_v="$1" local required_v="$1"
local getted_v="$2" local getted_v="$2"
# First: check if equal # First: check if equal
if [ "$required_v" = "$getted_v" ]; then if [ "$required_v" = "$getted_v" ]; then
return true; return 0;
fi fi
# Second: check if greater or lesser # Second: check if greater or lesser
local test_v=$(printf "%s\n%s" "$greater_v" "$lesser_v" | sort -V | head -n 1) local test_v
$test_v=$(printf "%s\n%s" "$required_v" "$getted_v" | sort -V | head -n 1)
case $test_v in case $test_v in
$getted_v) return true ;; $getted_v) return 0 ;;
$required_v) return false ;; $required_v) return 1 ;;
*) return false ;; *) return 2 ;;
esac esac
} }
# Test Functions # Test Functions
f_test_nano_version() { f_test_nano_version() {
local version="nano --version | cut -d ' ' -f 4" local version
return f_compare_version $G_NANO_VERSION $version $version=$(nano --version | cut -d ' ' -f 4)
return $(f_compare_version $G_NANO_VERSION $version)
} }
f_test_shellcheck_version() { f_test_shellcheck_version() {
local version="nano --version | cut -d ' ' -f 8" local version
return f_compare_version $G_SHELLCHECK_VERSION $version $version=$(nano --version | cut -d ' ' -f 8)
return $(f_compare_version $G_SHELLCHECK_VERSION $version)
} }
f_test_nano_version f_test_nano_version
f_test_shellcheck_version f_test_shellcheck_version
# ....shellcheck -f diff *.sh | git apply | git commit -a -m "Shellcheck fast corrections" # ....shellcheck -f diff *.sh | git apply | git commit -a -m "Shellcheck fast corrections"
shellcheck *.sh shellcheck -- *.sh