fork download
  1. #!/usr/bin/env bash
  2. case $BASH_VERSION in [123].*) echo "ERROR: Requires bash 4.0 or newer" >&2; exit 1;; esac
  3.  
  4. PS4=':$LINENO+'; set -x
  5.  
  6. conn_re='[[:space:]]*conn[[:space:]]+([^[:space:]].*)'
  7. rightsource_re='[[:space:]]*(effective)?right(source|subnet)s?=(.*)'
  8. multimatch_re='[{](.*,.*)[}]'
  9.  
  10. conn=''
  11.  
  12. declare -A subnets=( )
  13.  
  14. while IFS= read -r line; do : line="$line"
  15. [[ $line =~ $conn_re ]] && { conn=${BASH_REMATCH[1]}; continue; }
  16. [[ $line =~ $rightsource_re ]] && { subnets[$conn]=${BASH_REMATCH[3]}; }
  17. done
  18. declare -p subnets >&2
  19.  
  20. handle_result() {
  21. local conn=$1 element=$2
  22. element=${element%/32}
  23. echo "For conn $conn, need to handle element $element"
  24. }
  25.  
  26. for conn in "${!subnets[@]}"; do
  27. : conn="$conn"
  28. rightsource=${subnets[$conn]}
  29. if [[ $rightsource =~ $multimatch_re ]]; then
  30. IFS=, read -r -a elements <<<"${BASH_REMATCH[1]}"
  31. for element in "${elements[@]}"; do
  32. handle_result "$conn" "$element"
  33. done
  34. else
  35. handle_result "$conn" "$rightsource"
  36. fi
  37. done
Success #stdin #stdout #stderr 0.01s 4384KB
stdin
conn example
        rightsubnets={44.45.46.79/32,44.45.46.80/32}
conn next_example
        rightsubnets=1.2.3.4
conn third_example
        rightsubnets={5.6.7.8,9.10.11.12/32}
stdout
For conn example, need to handle element 44.45.46.79
For conn example, need to handle element 44.45.46.80
For conn third_example, need to handle element 5.6.7.8
For conn third_example, need to handle element 9.10.11.12
For conn next_example, need to handle element 1.2.3.4
stderr
:6+conn_re='[[:space:]]*conn[[:space:]]+([^[:space:]].*)'
:7+rightsource_re='[[:space:]]*(effective)?right(source|subnet)s?=(.*)'
:8+multimatch_re='[{](.*,.*)[}]'
:10+conn=
:12+subnets=()
:12+declare -A subnets
:14+IFS=
:14+read -r line
:14+: 'line=conn example'
:15+[[ conn example =~ [[:space:]]*conn[[:space:]]+([^[:space:]].*) ]]
:15+conn=example
:15+continue
:14+IFS=
:14+read -r line
:14+: 'line=        rightsubnets={44.45.46.79/32,44.45.46.80/32}'
:15+[[         rightsubnets={44.45.46.79/32,44.45.46.80/32} =~ [[:space:]]*conn[[:space:]]+([^[:space:]].*) ]]
:16+[[         rightsubnets={44.45.46.79/32,44.45.46.80/32} =~ [[:space:]]*(effective)?right(source|subnet)s?=(.*) ]]
:16+subnets[$conn]='{44.45.46.79/32,44.45.46.80/32}'
:14+IFS=
:14+read -r line
:14+: 'line=conn next_example'
:15+[[ conn next_example =~ [[:space:]]*conn[[:space:]]+([^[:space:]].*) ]]
:15+conn=next_example
:15+continue
:14+IFS=
:14+read -r line
:14+: 'line=        rightsubnets=1.2.3.4'
:15+[[         rightsubnets=1.2.3.4 =~ [[:space:]]*conn[[:space:]]+([^[:space:]].*) ]]
:16+[[         rightsubnets=1.2.3.4 =~ [[:space:]]*(effective)?right(source|subnet)s?=(.*) ]]
:16+subnets[$conn]=1.2.3.4
:14+IFS=
:14+read -r line
:14+: 'line=conn third_example'
:15+[[ conn third_example =~ [[:space:]]*conn[[:space:]]+([^[:space:]].*) ]]
:15+conn=third_example
:15+continue
:14+IFS=
:14+read -r line
:14+: 'line=        rightsubnets={5.6.7.8,9.10.11.12/32}'
:15+[[         rightsubnets={5.6.7.8,9.10.11.12/32} =~ [[:space:]]*conn[[:space:]]+([^[:space:]].*) ]]
:16+[[         rightsubnets={5.6.7.8,9.10.11.12/32} =~ [[:space:]]*(effective)?right(source|subnet)s?=(.*) ]]
:16+subnets[$conn]='{5.6.7.8,9.10.11.12/32}'
:14+IFS=
:14+read -r line
:18+declare -p subnets
declare -A subnets=([example]="{44.45.46.79/32,44.45.46.80/32}" [third_example]="{5.6.7.8,9.10.11.12/32}" [next_example]="1.2.3.4" )
:26+for conn in "${!subnets[@]}"
:27+: conn=example
:28+rightsource='{44.45.46.79/32,44.45.46.80/32}'
:29+[[ {44.45.46.79/32,44.45.46.80/32} =~ [{](.*,.*)[}] ]]
:30+IFS=,
:30+read -r -a elements
:31+for element in "${elements[@]}"
:32+handle_result example 44.45.46.79/32
:21+local conn=example element=44.45.46.79/32
:22+element=44.45.46.79
:23+echo 'For conn example, need to handle element 44.45.46.79'
:31+for element in "${elements[@]}"
:32+handle_result example 44.45.46.80/32
:21+local conn=example element=44.45.46.80/32
:22+element=44.45.46.80
:23+echo 'For conn example, need to handle element 44.45.46.80'
:26+for conn in "${!subnets[@]}"
:27+: conn=third_example
:28+rightsource='{5.6.7.8,9.10.11.12/32}'
:29+[[ {5.6.7.8,9.10.11.12/32} =~ [{](.*,.*)[}] ]]
:30+IFS=,
:30+read -r -a elements
:31+for element in "${elements[@]}"
:32+handle_result third_example 5.6.7.8
:21+local conn=third_example element=5.6.7.8
:22+element=5.6.7.8
:23+echo 'For conn third_example, need to handle element 5.6.7.8'
:31+for element in "${elements[@]}"
:32+handle_result third_example 9.10.11.12/32
:21+local conn=third_example element=9.10.11.12/32
:22+element=9.10.11.12
:23+echo 'For conn third_example, need to handle element 9.10.11.12'
:26+for conn in "${!subnets[@]}"
:27+: conn=next_example
:28+rightsource=1.2.3.4
:29+[[ 1.2.3.4 =~ [{](.*,.*)[}] ]]
:35+handle_result next_example 1.2.3.4
:21+local conn=next_example element=1.2.3.4
:22+element=1.2.3.4
:23+echo 'For conn next_example, need to handle element 1.2.3.4'