#!/bin/bash # Dynamically modifies openssl config file # ./inimodify.sh [header] [variable] [value] < openssl.cnf # ./inimodify.sh "usr_cert" "extendedKeyUsage" "clientAuth, serverAuth" < openssl.cnf #requires bash 4.0 regexes set -x set -e [ -n "$1" ] && HEADER="$1" || exit 2 [ -n "$2" ] && VARIABLE="$2" || exit 3 [ -n "$3" ] && VALUE="$3" || exit 4 IFS=$'\n' HEADER_STANZA=0 VARIABLE_MODIFIED=0 while read line; do if [[ "$line" =~ ^\ *\[\ *"$HEADER"\ *\] ]]; then HEADER_STANZA=1 echo "$line" elif [ "$HEADER_STANZA" == 1 ] && [[ "$line" =~ ^\ *"$VARIABLE"\ *= ]]; then echo -n "$line" echo ", $VALUE" VARIABLE_MODIFIED=1 elif [ "$HEADER_STANZA" == 1 ] && [[ "$line" =~ ^\ *\[\ .+\ \] ]]; then if [ "$VARIABLE_MODIFIED" == 0 ]; then echo "$VARIABLE = $VALUE" echo VARIABLE_MODIFIED=1 fi HEADER_STANZA=0 echo "$line" else echo "$line" fi done