Reputation: 19
I am writing a script to verify whether given cert is issued by my CA.The script takes the cert i wan to check as input. Below shared code snippet is what i am using,
output=$(openssl verify -CAfile /home/Admin/CA/sign_CA.pem $1 2> error.txt)
if [[ $output == *"error"* ]];then
echo "Certificate Verification Failed"
exit 1
fi
As you can see, i am redirecting error to error.txt file. Also output should be stored in the output variable.
When i pass a non-existing file, then i am getting the error printed in my screen. Also the regex is not working. I am not getting Certificate Verification Failed error message.
Output from my shell:
Admin@Bionic-WorkBook:~/CA$./verify.sh /home/Admin/ad21.pem
Can't open ../ad21.pem for reading, No such file or directory
139691424915904:error:02001002:system library:fopen:No such file or directory:../crypto/bio/bss_file.c:72:fopen('../ad21.pem','r')
139691424915904:error:2006D080:BIO routines:BIO_new_file:no such file:../crypto/bio/bss_file.c:79: unable to load certificate
Contents of error.txt:
Admin@Bionic-WorkBook:~/CA$cat output.txt
Can't open ../ad21.pem for reading, No such file or directory
140041413374400:error:02001002:system library:fopen:No such file or directory:../crypto/bio/bss_file.c:72:fopen('../ad21.pem','r')
140041413374400:error:2006D080:BIO routines:BIO_new_file:no such file:../crypto/bio/bss_file.c:79: unable to load certificate
My Questions:
i) How did error print in screen? As you can see error is redirected to file and output is to a variable. Then, How did the error print on screen?
i) Why did regex fail? As the output has the same error message, why did regex fail?
Upvotes: 0
Views: 249
Reputation: 337
You would find your answer to your doubts in this link :- https://superuser.com/a/935427
Here is TL;DR answer :-
$(command)
would store output of command
in variable output
for you.
Ex.
cat file.txt //all command's success failure can be check using $? variable.
echo $?
If you want to store error in output
variable then just don't do redirect or use something like this (check the highlighted part in code block)
output=$(openssl verify -CAfile /home/Admin/CA/sign_CA.pem $1 2> error.txt ; cat error.txt)
if [[ $output == *"error"* ]];then
echo "Certificate Verification Failed"
exit 1
fi
Upvotes: 0