Je cherche une aide d'aide à l'analyse d'une sous-chaîne de la sortie d'une commande.
La sortie de la commande ressemble à ceci (la commande est certificats de Cerbot-auto Code> et lié à LETSENCRYPT Certificats: P>
certificate="amadev.domain1.com"
domains="amadev.domain1.com,rcpdev8.domain1.com"
3 Réponses :
une solution de grep / coupe simple:
Essayez ceci ( shellcheck -clean) Code PURE BASH:
#! /bin/bash # Regular expressions to match the required fields name_rx='^Certificate[[:space:]]+Name:[[:space:]]+(.*)$' domains_rx='^Domains:[[:space:]]+(.*)$' certbot_auto_output=$(certbot-auto certificates) # (Assuming a standard $IFS) 'read' will strip leading and trailing whitespace while read -r line ; do if [[ $line =~ $name_rx ]] ; then certificate=${BASH_REMATCH[1]} elif [[ $line =~ $domains_rx ]] ; then domains=${BASH_REMATCH[1]} domains=${domains// /,} # Replace spaces with commas printf 'certificate="%s"\n' "$certificate" printf 'domains="%s"\n' "$domains" fi done <<<"$certbot_auto_output"
La réponse de FunkyJelly a utilisé ce qui suit:
# Store each certificate name and list of domains in an array mapfile -t certificate_names < <(/opt/certbot-auto certificates | grep 'Certificate Name:' | cut -d':' -f2) mapfile -t domains < <(/opt/certbot-auto certificates | grep 'Domains:' | cut -d':' -f2) len=${#certificate_names[@]} for (( i=0; i<$len; i++ )) do #replace spaces with commas and drop the first comma fqdns=${domains[$i]} fqdns=${fqdns// /,} fqdns="${fqdns:1}" # Renew the cert /opt/certbot-auto certonly --manual --agree-tos --manual-public-ip-logging-ok --email oueemail@example.com --preferred-challenges dns --manual-auth-hook update-txt-records.sh --manual-cleanup-hook clean-txt-records.sh --cert-name ${certificate_names[$i]} -d $fqdns --expand --noninteractive done