#=== FUNCTION ================================================================ # NAME: read_and_check # DESCRIPTION: Reads a value (see read_value) and checks it (see check_value) # against an arbitrary long list of Bash regular expressions # PARAMETERS: 1 - Prompt string for the read # 2 - Name of variable to receive the result # 3 - Default value (optional) # 4..n - Valid regular expressions # RETURNS: Nothing #=============================================================================== read_and_check () { local prompt="${1:?Usage: read_and_check prompt outputname [default] list_of_regex}" local outputname="${2:?Usage: read_and_check prompt outputname [default] list_of_regex}" local default="${3:-}" if ! read_value "$prompt" "$outputname" "$default"; then return 1 fi shift 3 until check_value "${!outputname}" "$@" do echo "Invalid input: ${!outputname}" if ! read_value "$prompt" "$outputname" "$default"; then return 1 fi done return 0 }