DHCPInfo/utils/myread.sh

34 lines
1.1 KiB
Bash
Raw Normal View History

2023-11-12 21:38:07 +00:00
#!/bin/bash
get_sections() {
local START_REGEX="${1:?"get_sections: START_REGEX was not provided"}"
local -n CONTAINER="${2:?"get_sections: Has CONTAINER variable not passed into function"}"
local CONTENT="${3:?"get_sections: Content was not provided"}"
local CURRENT_BUFFER=""
local -i OPEN_BRACKETS=0
local -i CLOSE_BRACKETS=0
local CAN_READ=0
while read line; do
if [[ "$line" =~ ^$START_REGEX.*$ ]]; then
CAN_READ=1
fi
if [[ $CAN_READ -eq 1 ]]; then
CURRENT_BUFFER+="$line\n"
[[ "$line" == *'{'* ]] && ((OPEN_BRACKETS++))
[[ "$line" == *'}'* ]] && ((CLOSE_BRACKETS++))
if [[ $OPEN_BRACKETS -eq $CLOSE_BRACKETS ]]; then
CONTAINER+=( "$( echo -e "$CURRENT_BUFFER" )" )
CURRENT_BUFFER=""
CAN_READ=0
OPEN_BRACKETS=0
CLOSE_BRACKETS=0
fi
fi
done <<< "$CONTENT"
}