#!/bin/bash validate_version() { local LC_ALL=C local pattern='^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z]+([.-][0-9A-Za-z]+)*)?(\+[0-9A-Za-z]+([.-][0-9A-Za-z]+)*)?$' [[ $# == 1 && $1 =~ $pattern ]] || { printf '%s\n' 'Invalid version: use X.Y.Z with optional prerelease and build metadata.' >&2 return 1 } } validate_build_number() { local LC_ALL=C [[ $# == 1 && $1 =~ ^[0-9]+$ ]] || { printf '%s\n' 'Invalid build number: use digits only.' >&2 return 1 } } validate_release_version() { [[ $# == 2 ]] && validate_version "$1" && validate_build_number "$2" } validate_ordered_version() { local LC_ALL=C validate_version "$@" || return 1 [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || { printf '%s\n' 'Release versions must be MAJOR.MINOR.PATCH.' >&2 return 1 } } validate_positive_build_number() { local LC_ALL=C validate_build_number "$@" || return 1 [[ $1 =~ ^[1-9][0-9]*$ ]] || { printf '%s\n' 'Build numbers must be positive integers.' >&2 return 1 } } if [[ ${BASH_SOURCE[0]} == "$0" ]]; then command=${1:-} if (( $# )); then shift; fi case "$command" in version) validate_version "$@" ;; build) validate_build_number "$@" ;; release) validate_release_version "$@" ;; ordered-version) validate_ordered_version "$@" ;; positive-build) validate_positive_build_number "$@" ;; *) printf 'Usage: %s version VALUE | build VALUE | release VERSION BUILD | ordered-version VALUE | positive-build VALUE\n' "$0" >&2; exit 2 ;; esac fi