From 1e0b0b090866fb6f30d806b2c7f03ca10b106aa3 Mon Sep 17 00:00:00 2001 From: Bryan Roessler Date: Wed, 20 Oct 2021 17:08:41 -0400 Subject: [PATCH] Add custom readarray --- functions | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/functions b/functions index 1cb0302a..1816fd28 100644 --- a/functions +++ b/functions @@ -13,4 +13,39 @@ ask_ok() { is_root() { user=$(whoami) [[ $user != "root" ]] && echo "Script must be run with sudo rights!" && exit 1 -} \ No newline at end of file +} + +if ! type -t readarray >/dev/null; then + readarray() { + local cmd opt t v=MAPFILE + while [ -n "$1" ]; do + case "$1" in + -h|--help) echo "minimal substitute readarray for older bash"; exit; ;; + -r) shift; opt="$opt -r"; ;; + -t) shift; t=1; ;; + -u) + shift; + if [ -n "$1" ]; then + opt="$opt -u $1"; + shift + fi + ;; + *) + if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then + v="$1" + shift + else + echo -en "${C_BOLD}${C_RED}Error: ${C_RESET}Unknown option: '$1'\n" 1>&2 + exit + fi + ;; + esac + done + cmd="read $opt" + eval "$v=()" + while IFS= eval "$cmd line"; do + line=$(echo "$line" | sed -e "s#\([\"\`]\)#\\\\\1#g" ) + eval "${v}+=(\"$line\")" + done + } +fi \ No newline at end of file