Skip to main content
added 8 characters in body
Source Link
choroba
  • 49.7k
  • 7
  • 92
  • 119

Use an associative array to store the certificate names per role and environment.

#! /bin/bash

unset -v envs
declare -A envs

while IFS='| ' read -r certname role env; do
    envs["$role.$env"]+="$certname"$'\n' 
done < /tmp/inventory.list

for e in "${!envs[@]}" ; do
    printf '%s\n' "[$e]" "${envs[$e]}"
done

To sort the sections, you can print the keys, sort them, and then read them back and output the associated values:

for e in "${!envs[@]}" ; do
    echoprintf '%s\n' "$e"
done | sort | while read -r e ; do
    echo "[$e]"
  printf '%s\n' echo"[$e]" "${envs[$e]}"
done

Use an associative array to store the certificate names per role and environment.

#! /bin/bash

unset -v envs
declare -A envs

while IFS='| ' read -r certname role env; do
    envs["$role.$env"]+="$certname"$'\n' 
done < /tmp/inventory.list

for e in "${!envs[@]}" ; do
    printf '%s\n' "[$e]" "${envs[$e]}"
done

To sort the sections, you can print the keys, sort them, and then read them back and output the associated values:

for e in "${!envs[@]}" ; do
    echo "$e"
done | sort | while read -r e ; do
    echo "[$e]"
    echo "${envs[$e]}"
done

Use an associative array to store the certificate names per role and environment.

#! /bin/bash

unset -v envs
declare -A envs

while IFS='| ' read -r certname role env; do
    envs["$role.$env"]+="$certname"$'\n' 
done < /tmp/inventory.list

for e in "${!envs[@]}" ; do
    printf '%s\n' "[$e]" "${envs[$e]}"
done

To sort the sections, you can print the keys, sort them, and then read them back and output the associated values:

for e in "${!envs[@]}" ; do
    printf '%s\n' "$e"
done | sort | while read -r e ; do
    printf '%s\n' "[$e]" "${envs[$e]}"
done
sorting
Source Link
choroba
  • 49.7k
  • 7
  • 92
  • 119

Use an associative array to store the certificate names per role and environment.

#! /bin/bash

unset -v envs
declare -A envs

while IFS='| ' read -r certname role env; do
    envs["$role.$env"]+="$certname"$'\n' 
done < /tmp/inventory.list

for e in "${!envs[@]}" ; do
    printf '%s\n' "[$e]" "${envs[$e]}"
done

To sort the sections, you can print the keys, sort them, and then read them back and output the associated values:

for e in "${!envs[@]}" ; do
    echo "$e"
done | sort | while read -r e ; do
    echo "[$e]"
    echo "${envs[$e]}"
done

Use an associative array to store the certificate names per role and environment.

#! /bin/bash

unset -v envs
declare -A envs

while IFS='| ' read -r certname role env; do
    envs["$role.$env"]+="$certname"$'\n' 
done < /tmp/inventory.list

for e in "${!envs[@]}" ; do
    printf '%s\n' "[$e]" "${envs[$e]}"
done

Use an associative array to store the certificate names per role and environment.

#! /bin/bash

unset -v envs
declare -A envs

while IFS='| ' read -r certname role env; do
    envs["$role.$env"]+="$certname"$'\n' 
done < /tmp/inventory.list

for e in "${!envs[@]}" ; do
    printf '%s\n' "[$e]" "${envs[$e]}"
done

To sort the sections, you can print the keys, sort them, and then read them back and output the associated values:

for e in "${!envs[@]}" ; do
    echo "$e"
done | sort | while read -r e ; do
    echo "[$e]"
    echo "${envs[$e]}"
done
with bash and in the global context, you need to unset a variable before declaring in case it was imported from the environment. Removed unreliable echo usage
Source Link
Stéphane Chazelas
  • 586.9k
  • 96
  • 1.1k
  • 1.7k

Use an associative array to store the certificate anmesnames per role and environment.

#! /bin/bash 

unset -v envs
declare -A envs

while IFS='| ' read -r certname role env; do
    envs["$role.$env"]+="$certname"$'\n' 
done < /tmp/inventory.list

for e in "${!envs[@]}" ; do
    echo "[$e]"
  printf '%s\n' echo"[$e]" "${envs[$e]}"
done

Use an associative array to store the certificate anmes per role and environment.

#! /bin/bash

declare -A envs

while IFS='| ' read -r certname role env; do
    envs["$role.$env"]+="$certname"$'\n' 
done < /tmp/inventory.list

for e in "${!envs[@]}" ; do
    echo "[$e]"
    echo "${envs[$e]}"
done

Use an associative array to store the certificate names per role and environment.

#! /bin/bash 

unset -v envs
declare -A envs

while IFS='| ' read -r certname role env; do
    envs["$role.$env"]+="$certname"$'\n' 
done < /tmp/inventory.list

for e in "${!envs[@]}" ; do
    printf '%s\n' "[$e]" "${envs[$e]}"
done
Source Link
choroba
  • 49.7k
  • 7
  • 92
  • 119
Loading