0

I'm compiling Nginx from source on Debian 12 in Bash. I'd like to select the TLS vendor (e.g. LibreSSL, OpenSSL) via a preflight variable ($nginx_tls_vendor). An example configure command without the variable stuff looks like this:

./configure \
--with-foo \
[…]
--with-openssl=/src/vendor/library-1.2.3 \
--with-openssl-opt="baz" \
[…]
--with-bar \

The values of the --with-openssl and --with-openssl-opt flags depend on $nginx_tls_vendor. Broadly like this for each vendor:

if [ "$nginx_tls_vendor" = "libressl" ] \
; then \
--with-openssl=/src/libressl/libressl-1.2.3 \
--with-openssl-opt="baz-libressl etc" \
; fi 
if [ "$nginx_tls_vendor" = "openssl" ] \
; then \
--with-openssl=/src/openssl/openssl-3.2.1 \
--with-openssl-opt="baz-openssl etc" \
; fi 

I haven't yet figured out how to use one or more if checks inline within a command. I am reasonably comfortable with an if wrapper around an entire command, but this doesn't apply here. I would like to use two inline if checks to find the TLS vendor and apply the relevant flags.

I've tried various things that might work but they end up breaking my download-and-compile script.

I would be grateful for some guidance and / or further reading so I can integrate two consecutive if checks around the two --with-openssl and --with-openssl-opt flags.

Thank you.

  • 1
    Why all the \in the if line there? That makes it really hard to read. You can write if [ condition ]; then and just start a new line, or if [ condition ] (without the ;, that isn't needed if you add a newline) and have the then in a new line, and you don't need then \, you can just move straight on to the next line. None of this is related to your question, just pointing it out FYI. – terdon Mar 30 '24 at 21:48
  • @terdon Thank you for your comment and guidance on the punctuation. I am (overly) verbose as I have all my build scripts as Markdown files in a repository and I copy / paste into a terminal window. The slashes help me to understand as I'm learning, and it's more granular on the change control side of things so I can see exactly what part(s) I've updated in my commit history. The Nginx configure flag stuff is split over ~60 lines in my IDE, so I can add / remove stuff in testing with a full history of what I've done. – Pete Cooper Mar 31 '24 at 11:32
  • 1
    OK. But my pint was that you can still split over many lines, the slashes are not needed. But they are not wrong, so if you really prefer it, that's your call. – terdon Mar 31 '24 at 14:06

1 Answers1

4

There are some ways to put conditionals inline with the command, but really, it's probably best to just build the list of arguments in an array (at least the varying ones) and then use the array when running the program. As long as you're using Bash anyway, since arrays aren't a standard feature. (See links below for alternative solutions.)

If you're just testing a single variable against a set of values, the case stament might be nicer than an if. (Not that there's anything wrong with if, either.)

The main parts are the args+=(foo) assignment to append to the array, and the array expansion "${args[@]}".

Something like this:

args=()
case "$nginx_tls_vendor" in
    libressl) 
        args+=(
        --with-openssl=/src/libressl/libressl-1.2.3
        --with-openssl-opt="baz-libressl etc"
        );;
    openssl) 
        args+=(
        --with-openssl=/src/openssl/openssl-3.2.1
        --with-openssl-opt="baz-openssl etc"
        );;
    *) 
        echo >&2 "error: invalid value of 'nginx_tls_vendor'"
        exit 1;;
esac

./configure --with-foo "${args[@]}" --with-bar

Or, you could even apply associative arrays (again in Bash):

declare -A openssl_libs=(
    [libressl]=/src/libressl/libressl-1.2.3
    [openssl]=/src/openssl/openssl-3.2.1
)
declare -A openssl_opts=(
    [libressl]="baz-libressl etc"
    [openssl]="baz-openssl etc"
)

./configure
--with-foo
--with-openssl="${openssl_libs[$nginx_tls_vendor]}"
--with-openssl-opt="${openssl_opts[$nginx_tls_vendor]}"
--with-bar \

#end

(Though watch for invalid values of $nginx_tls_vendor there.)

See also:

(While you could technically embed if staments within a command substitution directly in the argument list of configure, that would be awkward wrt. quoting and word splitting, slightly inefficient and probably somewhat difficult to read, too. Wouldn't suggest doing that.)

ilkkachu
  • 138,973