I'm writing a Bash shell script to automatically add SSL for domains from the command line. Earlier in the script, the user enters his/her domains in a space separated list. This list is added to an Nginx server block. My question is how to read this space separated list (string variable) into the certbot command, automatically adding -d before each domain. I figure this can be done with sed somehow but I don't know how. I think Certbot should support some input string or file format (either space separated or line separated) but it doesn't seem to.
You don't need a separate "-d" for each domain name. Just put all domain names separated by commas into a single list with quotes around it. Technically the quotes are optional, but generally good practice.
Thank you for your answer. That brings me to the question how to transform a space separated list into a comma separated list. If I would know how to do that, I figure I would also know how to replace each space by ' -d ' instead of a comma...
Programming related questions are better addressed at a forum for those. Such as stackoverflow.com
Or, even ask Google or chatGPT or Claude ![]()
Yeah, these kind of things I'd trust a LLM with
![]()
I feel your pain and can answer your question. I can't answer many questions. If this example command-line usage does not make sense, you need to back way up and reconsider your approach. Is a shell script appropriate for what you are doing? Shell scripting is the glue language and very useful for what it does.
The other question is whether or not your approach is appropriate. I can't tell. All I can say is that I have no idea what you are doing or why you are doing it. And that's fine. I'm not real smart about computers. I only know what I know.
[doug@burr ~]$ # Domain string with excessive regular spaces. Outer parentheses create subshell environment that will not alter this command-line environment..
[doug@burr ~]$ (d=' example.com www.example.com '; printf '%s\n' "$d")
example.com www.example.com
[doug@burr ~]$
[doug@burr ~]$ # Replace any internal run of 'space' with a single comma.
[doug@burr ~]$ (d=' example.com www.example.com '; printf '%s\n' "$d" | sed -E 's/([^[:space:]])[[:space:]]+([^[:space:]])/\1,\2/g')
example.com,www.example.com
[doug@burr ~]$
[doug@burr ~]$ # Remove unnecessary leading and trailing 'space'.
[doug@burr ~]$ (d=' example.com www.example.com '; printf '%s\n' "$d" | sed -E 's/^[[:space:]]+//'| sed -E 's/[[:space:]]+$//'| sed -E 's/([^[:space:]])[[:space:]]+([^[:space:]])/\1,\2/g')
example.com,www.example.com
[doug@burr ~]$
[doug@burr ~]$ d_comma_list=$(d=' example.com www.example.com '; printf '%s\n' "$d" | sed -E 's/^[[:space:]]+//'| sed -E 's/[[:space:]]+$//'| sed -E 's/([^[:space:]])[[:space:]]+([^[:space:]])/\1,\2/g')
[doug@burr ~]$
[doug@burr ~]$ echo $d_comma_list
example.com,www.example.com
[doug@burr ~]$
For more information on GNU sed see: Character Classes and Bracket Expressions (sed, a stream editor)
For more information on POSIX compliance for compatibility/portibility with Unix-family shells see: The Open Group Base Specifications Issue 8
I also wish certbot would take a file with a list of domains. Meanwhile, this is the snippet I use to turn my domain-list file into a comma-separated list, ignoring lines that start with # to allow comments:
domains=($(grep -v '^#' ./domains.txt))
domains_str=$(IFS=,; echo "${domains[*]}")
certbot ... -d "$domains_str" ...
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.