Search code examples
csvterminalgnupg

Search .cvs file of email addresses for public pgp-keys


I have a list of mail addresses of friends (.csv) and I want to see if they have public keys stored on pgp keyservers. I want to get this going for Mac.

The pgp part is not the problem, however I can't get my head around the for loop to go through each element in the file...

for add in {cat contacts.csv | grep @}; do gpg --search-keys $add; done

Solution

  • Don't write loops just for running a single command for each line of a file, use xargs instead. cat is also not required here.

    This small snippet is doing what you're trying to achieve:

    grep @ contacts.csv | xargs -n 1 gpg --search-keys
    

    If you insist in the loop, use the right parenthesis $( ... ) runs the command in a subshell):

    for add in $( grep @ contacts.csv ); do gpg --search-keys $add; done
    

    I answered a similar, but not equal question on the security stack exchange and Stack Overflow, you might also get some inspiration there.