Menu Home

Locating openssl on Solaris

Annoyingly, openssl can reside in a number of places on Solaris. I have a script that tries to call it, and initially I started with a very dirty if - elif cluster. I have long since committed it to the /dev/null in the sky, but think along the lines of:

if [[ -f /some/path/openssl ]]; then
  OpenSSL=/some/path/openssl
elif [[ -f /opt/another/path/openssl ]]; then
...

When I had some time, I revisited this disgusting little problem, and consolidated that down to this:

 
# Otherwise, we failover to openssl
elif ! command -v openssl &>/dev/null; then
  # Sigh, Solaris you pain in the ass
  for d in /usr/local/ssl/bin /opt/csw/bin /usr/sfw/bin; do
    if [ -f "${d}/openssl" ]; then
      OpenSSL="${d}/openssl"
    else
      OpenSSL=openssl
    fi
  done

Fairly straightforward, but not without its problems. But it worked reliably and was nicer than the previous effort.

One day, I happened upon this section of code and realised I could do it better still, so now it’s this:

elif ! command -v openssl &>/dev/null; then
  OpenSSL=$(command -v {,/usr/bin/,/usr/local/ssl/bin/,/opt/csw/bin/,/usr/sfw/bin/}openssl 2>/dev/null | head -1)

What this does is uses brace expansion, so command -v is trying to match: /usr/bin/openssl, /usr/local/ssl/bin/openssl, /opt/csw/bin/openssl, /usr/sfw/bin/openssl and plain old $PATH/openssl… like so:

$ command -v {,/usr/bin/,/usr/local/ssl/bin/,/opt/csw/bin/,/usr/sfw/bin/}openssl
bash: command: openssl: not found
bash: command: /usr/bin/openssl: not found
/usr/local/ssl/bin/openssl
bash: command: /opt/csw/bin/openssl: not found
bash: command: /usr/sfw/bin/openssl: not found

So we use 2>/dev/null to silence the not founds and head -1 to give us one found result. Et Voila!

Categories: Geeking Out Tech Tips

rawiri