Bash std
Provide the beginnings of a small bash standard library. This is intended to provide functionality which is absent from standard bash but is generally useful (i.e. it would be reasonable to expect this functionality in any given program rather than those that may be part of a common but still narrower domain).
The current code is cribbed from the GNU Bootstrap script.
std__me
Record the name by which this process was invoked (assuming typical invocation).
declare -r std__me="${0}"
std::warnf_
Emit the format string and any following arguments to stderr. This prefixes each line of a multi-line message with the script name.
std::warnf_ () {
warnf_format_=${1}
shift
nl='
'
case ${*} in
*${nl}*) me_=$(printf "${std__me}"|tr "${nl}|" '??')
printf "${warnf_format_}" "${@}" | sed "s|^|${me_}: |" ;;
*) printf "${std__me}: ${warnf_format_}" "${@}" ;;
esac >&2
}
std::warn_
Emit the arguments to stderr, sanitizing IFS as necessary.
std::warn_ () {
case ${IFS} in
' '*) std::warnf_ '%s\n' "${*}";;
*) (IFS=' '; std::warn_ "${@}");;
esac
}
std::die
Emit the arguments to stderr and exit process with error.
std::die() {
std::warn_ "${@}"
exit 1
}