linuxbashloopsgpg-signature

How to log gpg verified files?


I am running a gpg --verify on a list of files that I am referencing from gpg_verify.txt. I want to track which passed, so that I can later download some files that have first passed my check.

Therefore, how can I capture whether/or not a particular file in my loop passed/failed so that I can log it into a different file, that I can later reference?

I want something like:

while read -r line;
do 
  gpg --verify $line
  if(above output^ passes) then;
   > passed_gpg.txt
  else
   > failed_gpg.txt
done < gpg_verify.txt

Here is example output when I just run:

while read -r line;
do 
  gpg --verify $line
done < gpg_verify.txt

Output:

gpg: Signature made Tue Feb 11 17:26:10 2020 UTC
gpg:                using RSA key XXXXXXXXXXXX
gpg: Good signature from "Rando person (XXXXX Signing Key) <example@example.com>" [ultimate]

Solution

  • Consider:

    #!/usr/bin/env bash
    while IFS= read -r filename; do
      if gpg -v "$filename"; then
        printf '%s\n' "$filename" >&3
      else
        printf '%s\n' "$filename" >&4
      fi
    done <gpg_verify.txt 3>passed_gpg.txt 4>failed_gpg.txt
    

    What's different here?

    This still isn't perfect -- a better tool would probably avoid making the assumptions about filenames implicit in storing them in a line-oriented file -- but it's a place to start.