#!/usr/bin/env bash case $BASH_VERSION in ''|[1-3].*) echo "ERROR: Bash 4.0 or newer required" >&2; exit 1;; esac PS4=':$LINENO+'; set -x # enable trace logging w/ line numbers start_hostgroup_re='^define[[:space:]]+hostgroup[[:space:]]*[{]' kv_re='^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)?' end_re='^[[:space:]]*}' declare -A keys=( ) comments=( ) build_new_members() { local hostgroup_name=$1 old_members=$2 echo "New member list for $hostgroup_name" } in_hostgroup=0 while IFS= read -r line; do : "line=$line" if (( in_hostgroup )); then if [[ $line =~ $kv_re ]]; then keys[${BASH_REMATCH[1]}]=${BASH_REMATCH[2]} comments[${BASH_REMATCH[1]}]=${BASH_REMATCH[3]} elif [[ $line =~ $end_re ]]; then keys["members"]=$(build_new_members "${keys["hostgroup_name"]}" "${keys["members"]}") printf '%s\n' 'define hostgroup {' for key in "${!keys[@]}"; do : key="$key" value=${keys[$key]} comment=${comments[$key]} if [[ $comment ]]; then printf ' %-16s %s %s\n' "$key" "$value" "$comment" else printf ' %-16s %s\n' "$key" "$value" fi done printf '%s\n' '}' keys=( ); comments=( ); in_hostgroup=0 elif [[ $line ]]; then # warn about non-empty non-assignment lines printf 'WARNING: Unrecognized line in hostgroup: %s\n' "$line" >&2 fi else if [[ $line =~ $start_hostgroup_re ]]; then in_hostgroup=1 else printf '%s\n' "$line" fi fi done
define hostgroup {
hostgroup_name NA-servers ; The name of the hostgroup
alias NA region ; Long name of the group
members sample.com ; hosts belonging to this group
}
define hostgroup{
hostgroup_name FTP-server ; The name of the hostgroup
alias FTP NA region ; Long name of the group
members example.com
}
define hostgroup {
members New member list for NA-servers ; hosts belonging to this group
alias NA region ; Long name of the group
hostgroup_name NA-servers ; The name of the hostgroup
}
define hostgroup {
members New member list for FTP-server
alias FTP NA region ; Long name of the group
hostgroup_name FTP-server ; The name of the hostgroup
}
:5+start_hostgroup_re='^define[[:space:]]+hostgroup[[:space:]]*[{]'
:6+kv_re='^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)?'
:7+end_re='^[[:space:]]*}'
:9+keys=()
:9+comments=()
:9+declare -A keys comments
:16+in_hostgroup=0
:17+IFS=
:17+read -r line
:17+: 'line=define hostgroup {'
:18+(( in_hostgroup ))
:40+[[ define hostgroup { =~ ^define[[:space:]]+hostgroup[[:space:]]*[{] ]]
:41+in_hostgroup=1
:17+IFS=
:17+read -r line
:17+: 'line= hostgroup_name NA-servers ; The name of the hostgroup'
:18+(( in_hostgroup ))
:19+[[ hostgroup_name NA-servers ; The name of the hostgroup =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]='NA-servers '
:21+comments[${BASH_REMATCH[1]}]='; The name of the hostgroup'
:17+IFS=
:17+read -r line
:17+: 'line= alias NA region ; Long name of the group'
:18+(( in_hostgroup ))
:19+[[ alias NA region ; Long name of the group =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]='NA region '
:21+comments[${BASH_REMATCH[1]}]='; Long name of the group'
:17+IFS=
:17+read -r line
:17+: 'line= members sample.com ; hosts belonging to this group'
:18+(( in_hostgroup ))
:19+[[ members sample.com ; hosts belonging to this group =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]='sample.com '
:21+comments[${BASH_REMATCH[1]}]='; hosts belonging to this group'
:17+IFS=
:17+read -r line
:17+: line=
:18+(( in_hostgroup ))
:19+[[ '' =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:22+[[ '' =~ ^[[:space:]]*} ]]
:36+[[ -n '' ]]
:17+IFS=
:17+read -r line
:17+: 'line=}'
:18+(( in_hostgroup ))
:19+[[ } =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:22+[[ } =~ ^[[:space:]]*} ]]
::23+build_new_members 'NA-servers ' 'sample.com '
::12+local 'hostgroup_name=NA-servers ' 'old_members=sample.com '
::13+echo 'New member list for NA-servers '
:23+keys["members"]='New member list for NA-servers '
:24+printf '%s\n' 'define hostgroup {'
:25+for key in "${!keys[@]}"
:25+: key=members
:26+value='New member list for NA-servers '
:27+comment='; hosts belonging to this group'
:28+[[ -n ; hosts belonging to this group ]]
:29+printf ' %-16s %s %s\n' members 'New member list for NA-servers ' '; hosts belonging to this group'
:25+for key in "${!keys[@]}"
:25+: key=alias
:26+value='NA region '
:27+comment='; Long name of the group'
:28+[[ -n ; Long name of the group ]]
:29+printf ' %-16s %s %s\n' alias 'NA region ' '; Long name of the group'
:25+for key in "${!keys[@]}"
:25+: key=hostgroup_name
:26+value='NA-servers '
:27+comment='; The name of the hostgroup'
:28+[[ -n ; The name of the hostgroup ]]
:29+printf ' %-16s %s %s\n' hostgroup_name 'NA-servers ' '; The name of the hostgroup'
:34+printf '%s\n' '}'
:35+keys=()
:35+comments=()
:35+in_hostgroup=0
:17+IFS=
:17+read -r line
:17+: line=
:18+(( in_hostgroup ))
:40+[[ '' =~ ^define[[:space:]]+hostgroup[[:space:]]*[{] ]]
:43+printf '%s\n' ''
:17+IFS=
:17+read -r line
:17+: 'line=define hostgroup{'
:18+(( in_hostgroup ))
:40+[[ define hostgroup{ =~ ^define[[:space:]]+hostgroup[[:space:]]*[{] ]]
:41+in_hostgroup=1
:17+IFS=
:17+read -r line
:17+: 'line= hostgroup_name FTP-server ; The name of the hostgroup'
:18+(( in_hostgroup ))
:19+[[ hostgroup_name FTP-server ; The name of the hostgroup =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]='FTP-server '
:21+comments[${BASH_REMATCH[1]}]='; The name of the hostgroup'
:17+IFS=
:17+read -r line
:17+: 'line= alias FTP NA region ; Long name of the group'
:18+(( in_hostgroup ))
:19+[[ alias FTP NA region ; Long name of the group =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]='FTP NA region '
:21+comments[${BASH_REMATCH[1]}]='; Long name of the group'
:17+IFS=
:17+read -r line
:17+: 'line= members example.com'
:18+(( in_hostgroup ))
:19+[[ members example.com =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:20+keys[${BASH_REMATCH[1]}]=example.com
:21+comments[${BASH_REMATCH[1]}]=
:17+IFS=
:17+read -r line
:17+: 'line=}'
:18+(( in_hostgroup ))
:19+[[ } =~ ^[[:space:]]*([^[:space:];]+)[[:space:]]+([^;]+)(;.*)? ]]
:22+[[ } =~ ^[[:space:]]*} ]]
::23+build_new_members 'FTP-server ' example.com
::12+local 'hostgroup_name=FTP-server ' old_members=example.com
::13+echo 'New member list for FTP-server '
:23+keys["members"]='New member list for FTP-server '
:24+printf '%s\n' 'define hostgroup {'
:25+for key in "${!keys[@]}"
:25+: key=members
:26+value='New member list for FTP-server '
:27+comment=
:28+[[ -n '' ]]
:31+printf ' %-16s %s\n' members 'New member list for FTP-server '
:25+for key in "${!keys[@]}"
:25+: key=alias
:26+value='FTP NA region '
:27+comment='; Long name of the group'
:28+[[ -n ; Long name of the group ]]
:29+printf ' %-16s %s %s\n' alias 'FTP NA region ' '; Long name of the group'
:25+for key in "${!keys[@]}"
:25+: key=hostgroup_name
:26+value='FTP-server '
:27+comment='; The name of the hostgroup'
:28+[[ -n ; The name of the hostgroup ]]
:29+printf ' %-16s %s %s\n' hostgroup_name 'FTP-server ' '; The name of the hostgroup'
:34+printf '%s\n' '}'
:35+keys=()
:35+comments=()
:35+in_hostgroup=0
:17+IFS=
:17+read -r line