4

Take the following command (real example) :

~$ gpg --edit-key foo@bar.net showpref quit
(...)
[ultimate] (1). Foo Bar <foo@bar.net>
     Cipher: AES256, AES192, AES, CAST5, 3DES
     AEAD: 
     Digest: SHA256, SHA1, SHA384, SHA512, SHA224
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
     Features: MDC, AEAD, Keyserver no-modify
     Preferred keyserver: ldap://keyserver.pgp.com
~$

Piping the output into a | grep doesn't work :

~$ gpg --edit-key foo@bar.net showpref quit | grep Compression
(...)
[ultimate] (1). Foo Bar <foo@bar.net>
     Cipher: AES256, AES192, AES, CAST5, 3DES
     AEAD: 
     Digest: SHA256, SHA1, SHA384, SHA512, SHA224
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
     Features: MDC, AEAD, Keyserver no-modify
     Preferred keyserver: ldap://keyserver.pgp.com
~$

Is there a way to make that work ? E.g., the result I'd like to get is :

~$ gpg --edit-key foo@bar.net showpref quit | grep Compression
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
~$

[EDIT : what I tried so far ] :

@steeldriver, @RomeoNinov : I don't think redirecting stderr is going to do the trick. It doesn't look like gpg's output goes to stderr.

Basically, gpg is an interactive command, but launching gpg ... cmd1 cmd2 makes it non-interactive (e.g., gpg ... showpref quit is the same as doing interactively showpref followed by quit in gpg's shell).

@steeldriver :

~$ gpg --edit-key foo@bar.net showpref quit 2> >(grep Compression)
(...)
[ultimate] (1). Foo Bar <foo@bar.net>
     Cipher: AES256, AES192, AES, CAST5, 3DES
     AEAD: 
     Digest: SHA256, SHA1, SHA384, SHA512, SHA224
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
     Features: MDC, AEAD, Keyserver no-modify
     Preferred keyserver: ldap://keyserver.pgp.com
~$

@RomeoNinov :

~$ gpg --edit-key foo@bar.net showpref quit 2>&1| grep Compression
(...)
[ultimate] (1). Foo Bar <foo@bar.net>
     Cipher: AES256, AES192, AES, CAST5, 3DES
     AEAD: 
     Digest: SHA256, SHA1, SHA384, SHA512, SHA224
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
     Features: MDC, AEAD, Keyserver no-modify
     Preferred keyserver: ldap://keyserver.pgp.com
~$
ChennyStar
  • 1,743

1 Answers1

4

The solution is to use --batch. This will help gpg to send info to standard file handlers.

# gpg  --batch --edit-key user@example.net showpref quit 2>&1 |grep Com
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
Romeo Ninov
  • 17,484
  • When called without --batch, where does gpg --edit-key foo@bar.net showpref quit send it's output if not stdout or stderr? – Ed Morton Mar 18 '24 at 14:59
  • 1
    @EdMorton, did not check how it do this but it send to the "current" console/terminal, avoiding STDOUT and STDERR. Why? Have no idea :) – Romeo Ninov Mar 18 '24 at 15:21