1

i have the following bash code written to detect if a SSL certificate exists and if so to skip the creation of one.

i need to expand the list of detected files, so that the presence of any of them will skip the creation of the SSL certificate.

the full list of files are "trailers.cer" or "trailers.key" or "trailers.pem"

An alterntaive approach is after detection, prompt the user asking if they want to create SSL certifictes

file="assets/certificates/trailers.cer"
if [ -f "$file" ]; then
echo 'SSL Certificates already created' 
else
openssl req -new -nodes -newkey rsa:2048 -out ./assets/certificates/trailers.pem -keyout ./assets/certificates/trailers.key -x509 -days 7300 -subj "/C=US/CN=trailers.apple.com"
openssl x509 -in ./assets/certificates/trailers.pem -outform der -out ./assets/certificates/trailers.cer && cat ./assets/certificates/trailers.key >> ./assets/certificates/trailers.pem
fi

2 Answers 2

2

Assuming it's sufficient to exit the entire script,

for file in trailers.cer trailers.key /assets/certificates/trailers.pem; do
    test -f "$file" && exit 1 # or even 0?
done
# If you reach through here, none existed

I changed one of the items to an absolute path just to show how it's done. If the path is the same for all of the files, you could refactor to supply the path later instead; test -f "/assets/certificates/$file"

Sign up to request clarification or add additional context in comments.

Comments

1

You could put multiple conditions in the if using multiple test and || like this:

if test -f "$path1" || test -f "$path2" || test -f "$path3"; then
    ...
fi

When there are many files, using an array can be easier and more readable, like this:

#!/bin/bash

basedir=assets/certificates
files=(trailers.cer trailers.key trailers.pem)

found=
for file in ${files[@]}; do
    path="$basedir/$file"
    if [ -f "$path" ]; then
        echo SSL Certificates already created
        found=1
        break
    fi
done

if test ! "$found"; then
    openssl req -new -nodes -newkey rsa:2048 -out ./assets/certificates/trailers.pem -keyout ./assets/certificates/trailers.key -x509 -days 7300 -subj "/C=US/CN=trailers.apple.com"
    openssl x509 -in ./assets/certificates/trailers.pem -outform der -out ./assets/certificates/trailers.cer && cat ./assets/certificates/trailers.key >> ./assets/certificates/trailers.pem
fi

3 Comments

-o is deprecated; use test -f "$path1" || test -f "$path2" || test -f "$path3" instead.
Where is that written? In the bash that comes with my up to date Debian testing this is not mentioned in neither man test nor help test...
The POSIX specification (see the Application Usage section) marks it as obsolescent, due to ambiguities in parsing depending on what other arguments are used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.