This repository has been archived on 2024-08-13. You can view files and clone it, but cannot push or open issues or pull requests.
bunc/lib/if.sh
ayakael fca165b274
* Changed name for _log function to _msg, added bail() function.
* Created _ansi function library to set terminal properties like
  cursor position and text color.
* Created _if function library, a set of simple if statements.
  Added _if_array_contains, _if_is_integer, _if_is defined,
  if_has_value
2018-01-06 20:30:09 -09:00

57 lines
916 B
Bash

#!/bin/bash
# doc if {
#
# NAME
# if
#
# DESCRIPTION
# A collection of special if functions
#
# USAGE
# <function> <arg>
#
# FUNCTIONS
# _array_contains <var>" "<Array[@]>"
# Checks if variable exists in array
#
# _is_integer <var>
# Checks if variable is integer
# EXIT VALUES
# 0 Check succesful
# 1 Check failed
#
# }
_if_array_contains() {
local i=${1}; shift
local eArray=(${@})
for e in ${eArray[@]}; do
[[ "${e}" = "${i}" ]] && return 0
done
return 1
}
_if_is_integer () {
local iArray=(${@})
for i in ${iArray[@]}; do [[ ${i} =~ ^-?[0-9]+$ ]] && return 0; done
return 1
}
_if_is_defined () {
[[ ! ${!1} && ${!1-_} ]] && {
echo "$1 is not set, aborting." >&2
exit 1
}
}
_if_has_value () {
if is_defined $1; then
if [[ -n ${!1} ]]; then
return 0
fi
fi
return 1
}