4

Using shell script to delete all matching certificates.

I found a list of certificates using:

security find-certificate -c "certificatename" -a -Z|grep SHA-1|awk '{ print $NF }'

Output is like this:

$ bash delete-cert.sh 
62D58774F9329FA1F96A2BB0C090130305B879ED
94B149CFDF32BDB1C9C28179FB586291167EC235

Now, I want to run the delete command on each of those SHA-1 hashes, how can I do that?

sudo security delete-certificate -Z hash

I was doing like this,

hashes=$(security find-certificate -c "certificatename" -a -Z|grep SHA-1|awk '{ print $NF }')
for hash in $hashes; do
    echo deleting $hash
    sudo security delete-certificate -Z $hash
done

But I like the one line solution by #chaos

6

Use this:

security find-certificate -c "certificatename" -a -Z | \
  sudo awk '/SHA-1/{system("security delete-certificate -Z "$NF)}'

awk is called with sudo. awk then searches for the string SHA-1 and calls the security delete-certificate command with the hash as argument. You have to provide the sudo password only once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.