Reputation: 33
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
Upvotes: 1
Views: 99
Reputation: 189357
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"
Upvotes: 2
Reputation: 124646
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
Upvotes: 1