5

Take the following command (real example) :

~$ gpg --edit-key [email protected] showpref quit
(...)
[ultimate] (1). Foo Bar <[email protected]>
     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 [email protected] showpref quit | grep Compression
(...)
[ultimate] (1). Foo Bar <[email protected]>
     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 [email protected] 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 [email protected] showpref quit 2> >(grep Compression)
(...)
[ultimate] (1). Foo Bar <[email protected]>
     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 [email protected] showpref quit 2>&1| grep Compression
(...)
[ultimate] (1). Foo Bar <[email protected]>
     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
~$
4
  • 1
    Are you sure the issue here is a subshell - rather than How to grep standard error stream (stderr)? Commented Mar 16, 2024 at 13:45
  • Have you try: gpg --edit-key [email protected] showpref quit 2>&1| grep Compression? Commented Mar 16, 2024 at 13:53
  • @steeldriver : I've seen that page before, and tried it, but it didn't work (see my edit). It doesn't look like gpg's output goes to stderr. Commented Mar 16, 2024 at 14:22
  • @RomeoNinov : yes, see my EDIT. Same result, not working. Commented Mar 16, 2024 at 14:22

1 Answer 1

5

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

# gpg  --batch --edit-key [email protected] showpref quit 2>&1 |grep Com
     Compression: ZLIB, BZIP2, ZIP, Uncompressed
2
  • When called without --batch, where does gpg --edit-key [email protected] showpref quit send it's output if not stdout or stderr? Commented Mar 18, 2024 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 :) Commented Mar 18, 2024 at 15:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.