As requested here's an example. Let's assume I want to collect backup data on a system that I admin. I only want this backup data to be recoverable by myself. To achieve this I want all data collected in the script to be encrypted with my public PGP key. I'm also assuming that the user account doing the backup isn't used for anything beyond backups so there's nothing important in the ~/.gnupg directory and that I can delete it without worrying about lost keys.
ALWAYS BACKUP your .gnupg directory before playing around with this!
The following is a simple bash script that will encrypt some text with a public key that's embedded in the script:
#!/bin/bash -e
gpg --trust-model always --import <<EOF
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.12 (GNU/Linux)
xxxxxx
...
...
-----END PGP PUBLIC KEY BLOCK-----
EOF
echo "backup data" | gpg --trust-model always --armor --encrypt --recipient [email protected] > backup-data.enc
The 'magic' in this script is the use of the shell 'Here Document' (see the EOF markers) to embed my public key in the script while passing it to gpg over stdin to import my key before doing the encryption / backup task. Naturally you'll have to put all of your ASCII armored public key in place of the '...'s in my example. Also be sure to replace '[email protected]' with the ID associated with your public key. Typically this is your email.
There's a million ways to clean this example up and make it more useful. Importing the public key on each run is a bit ridiculous so checking to see if the key is already in the ring is where I'd start :)
Good luck!